How to idiomatically alias a crate in Rust 2018?

This can be achieved with the rename-dependency Cargo feature, available in Rust 1.31. With this feature, it’s possible to provide a package attribute to the dependencies:

The rename-dependency feature allows you to import a dependency with a different name from the source. This can be useful in a few scenarios:

  • Depending on crates with the same name from different registries.
  • Depending on multiple versions of a crate.
  • Avoid needing extern crate foo as bar in Rust source.

Instead of writing

[dependencies]
foo_sys = "0.2"

the package key can be added to the dependency in Cargo.toml:

[dependencies]
foo = { package = "foo_sys", version = "0.2" }

WARNING: Cargo prior to Rust 1.26.0 may download the wrong dependency when using this feature!

Leave a Comment