在我的项目中,我有 2 个功能:myfeatureA
和 myfeatureB
。
我希望其中一个依赖于具有 rt
和 sync
功能的 tokio
,另一个依赖于 tokio
仅具有 sync
功能。
我在Cargo.toml
中尝试了此配置:
[dependencies]
tokio = { version = "1.32", features = ["rt", "sync"], optional = true }
[features]
myfeatureA = ["dep:tokio"]
myfeatureB = ["dep:tokio/sync"]
$ cargo build --features=myfeatureB
error: failed to parse manifest at `[...]/myproject/Cargo.toml`
Caused by:
feature `myfeatureB` includes `dep:tokio/sync` with both `dep:` and `/`
To fix this, remove the `dep:` prefix.
所以,我删除了 dep:
前缀(这是这个 other question 提供的解决方案,但它不起作用):
[features]
myfeatureA = ["dep:tokio"]
myfeatureB = ["tokio/sync"]
$ cargo build --features=myfeatureB
error: Package `myproject v0.1.0 ([...]/myproject)` does not have feature `tokio`. It has an optional dependency with that name, but that dependency uses the "dep:" syntax in the features table, so it does not have an implicit feature with that name.
如何使 myfeatureB
依赖于 tokio
仅具有 sync
功能?
最佳答案
这已在 issue #10788 中报道过,已在 Rust 1.72.0 中修复。现在您可以使用第二种方法(无 dep:
)。
对于以前的版本,根本不要使用 dep:
,即使对于其他功能也是如此。不过,这会创建一个名为 tokio
的功能。
关于rust - cargo .toml : how to conditionally enable a dependency feature?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77057406/