如何在Rust 2018中习惯性地使用板条箱别名? [英] How to idiomatically alias a crate in Rust 2018?
问题描述
我有一个板条箱foo_sys
.在Rust 2015中,为了方便起见,我使用extern crate foo_sys as foo
,但是在Rust 2018中,不再需要extern crate
,我不想仅将其用于别名.放下extern crate
时,我得到
I have a crate foo_sys
. In Rust 2015 I used extern crate foo_sys as foo
for convenience, but in Rust 2018 extern crate
isn't needed anymore and I don't want to use it only for aliasing. When dropping extern crate
, I get
错误 [E0463] :找不到
foo
推荐答案
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:
- 取决于来自不同注册表的同名箱子.
- 取决于板条箱的多个版本.
- 避免在Rust源中使用
extern crate foo as bar
.
代替写作
[dependencies]
foo_sys = "0.2"
package
键可以添加到Cargo.toml
中的依赖项:
the package
key can be added to the dependency in Cargo.toml
:
[dependencies]
foo = { package = "foo_sys", version = "0.2" }
这篇关于如何在Rust 2018中习惯性地使用板条箱别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!