如何激活可选依赖项?

How to activate an optional dependency?

Cargo.toml:

[features]
parallel = ["rayon"]

[dependencies.rayon]
version = "1.5"
optional = true

lib.rs:

#[cfg(feature = "parallel")]
pub mod par;

Rust 分析器:

code is inactive due to #[cfg] directives: feature = "parallel" is disabled

如何启用可选依赖?

您可以将 Rust Analyzer 配置选项 rust-analyzer.cargo.features 设置为包含您希望 RA 认为活跃的功能列表的数组。

您还可以将 rust-analyzer.cargo.allFeatures 设置为 true,以启用项目中的所有功能。

设置这些的方法根据您使用的 IDE 而有所不同 - 例如,如果使用 VS Code,您可以通过 Rust Analyzer 的“扩展设置”进行设置。

这假设您正在询问如何激活 Rust Analyzer 中的功能 - 在构建时激活它们或从 Cargo 运行,只需使用 --features 选项。

参见:Rust Analyzer Manual

所有非默认功能都需要在 运行ning 时指定。所以 运行 与:

# Option 1
cargo run --features parallel
# Option 2
cargo run --all-features

查看 cargo run --help 以获得更多帮助。