gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 07:43:26 25 4
gpt4 key购买 nike

我有一个 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/50999616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com