我有一个板条箱
A
,它有一个 std 和一个 no_std 变体。它们通过名为“std”的功能来区分,默认情况下:
[features]
default = ["std"]
std = []
现在我想创建一个板条箱
B
,它在std和no_std之间具有相同的区别,并且它取决于A
。现在,如果 B
设置了“std”,我也想将 A
与“std”设置一起使用,如果 B 中未设置“std”,我希望依赖项 A
也没有功能“std”设置。
我怎样才能实现这个目标?我想也许 [target.'cfg(...)'] 并根据功能的存在定义
A
的两个依赖项变体可能会有所帮助,但语法不允许功能作为键,而且似乎它会根本不会发生:https://github.com/rust-lang/cargo/issues/8170。
作为依赖功能下的货物簿文档:
依赖关系的功能也可以在
表中启用。语法是[features]
。例如:"package-name/feature-name"
[dependencies] jpeg-decoder = { version = "0.1.20", default-features = false } [features] # Enables parallel processing support by enabling the "rayon" feature of jpeg-decoder. parallel = ["jpeg-decoder/rayon"]
因此,在你的板条箱 B 的
Cargo.toml
中:
[dependencies]
a = { version = "...", default-features = false }
[features]
default = ["std"]
std = ["a/std"]