pro*_*osc 1 dependencies rust rust-cargo toml
场景如下:我的 crate 依赖于num-bigint,并且可选依赖于rand:
[dependencies]
num-bigint = { version = "0.2" }
rand = { version = "0.7", optional = true }
Run Code Online (Sandbox Code Playgroud)
什么时候 rand在我的箱子上被禁用时,一切都很好。
当rand在我的箱子上启用时,我希望也启用该rand功能num-bigint。我怎样才能做到这一点?
这是我尝试过的:
[target.'cfg(feature = "rand")'.dependencies]
num-bigint = { version = "0.2", features = ["rand"] }
Run Code Online (Sandbox Code Playgroud)
这有效,但我收到此警告:
warning: Found `feature = ...` in `target.'cfg(...)'.dependencies`. This key is not supported for selecting dependencies and will not work as expected. Use the [features] section instead: https://doc.rust-lang.org/cargo/reference/features.html
Run Code Online (Sandbox Code Playgroud)
我应该忽略警告,还是有更好的方法来做到这一点?我检查了该网页,但找不到任何有用的信息。
您可以"crate/feature"在您的功能中使用(如Cargo 文档中所述)来指定应启用的依赖项功能:
[features]
enable-rand = ["rand", "num-bigint/rand"]
Run Code Online (Sandbox Code Playgroud)
请注意,该功能的名称需要与依赖项的名称不冲突,因为可选的依赖项会创建隐式功能,并且您不能通过这种方式设置这些功能以启用其他功能。(如果您有兴趣,正在 Cargo issue #5565 中跟踪此功能的实现。)