rust - 如何在 Rust 2018 中惯用地为箱子起别名?

标签 rust rust-cargo rust-2018

我有一个 crate foo_sys。在 Rust 2015 中,为了方便起见,我使用 extern crate foo_sys as foo,但在 Rust 2018 中,不再需要 extern crate,我不想仅将其用于别名.当删除 extern crate 时,我得到

error[E0463]: can't find crate for foo

最佳答案

这可以通过 rename-dependency 来实现 cargo 功能,available in Rust 1.31 .使用此功能,可以为依赖项提供包属性:

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.

代替写作

[dependencies]
foo_sys = "0.2"

可以将 package 键添加到 Cargo.toml 中的依赖项:

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

警告:Cargo prior to Rust 1.26.0 may download the wrong dependency使用此功能时!

关于rust - 如何在 Rust 2018 中惯用地为箱子起别名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53311325/

相关文章:

rust - 什么是处理实际数据而不关心数据如何被保护/包装的惯用方式?

rust - |x| 的类型是什么移动|y| x+y?

rust - 如何阻止 `cargo clippy` 运行依赖项?

windows - 检查文件是否是 Windows 上 Rust 2018 中的符号链接(symbolic link)

rust - 如何在不使用 extern crate 的情况下导入 Rust 2018 中的所有宏、派生和过程宏?

rust - 预期的关闭,发现不同的关闭

io - 如何从 Rust 1.0 中的缓冲区读取整数?

rust - 在解析包版本时,我可以强制使用依赖项的 Cargo.lock 吗?

documentation - 如何方便地托管 crate 的最新文档?