rust - cargo .toml : how to conditionally enable a dependency feature?

标签 rust dependencies rust-cargo

在我的项目中,我有 2 个功能:myfeatureAmyfeatureB

我希望其中一个依赖于具有 rtsync 功能的 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/

相关文章:

rust - get_value 返回 `f64` 而不是 `[u8; 4]`

maven - OSGI Bundle vs jar 依赖

javascript - Highcharts 的导航器和依赖箭头 (ganttChart)

macos - 如何从 Mac 交叉编译到 Linux?

rust - 无法使用字节顺序更新注册表

generics - 通过关联类型返回泛型

rust - 为实现特定类型的 trait 的泛型类型实现 trait

rust - 使用 Tokio 启动多线程

dependencies - "Unable to resolve module"在 native react

assembly - 如何从 Cargo 构建中获取 assembly 输出?