Search code examples
rustrust-cargo

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 Analyzer:

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

How to enable the optional dependency?


Solution

  • You can set the Rust Analyzer configuration option rust-analyzer.cargo.features to an array containing a list of features that you want RA to consider active.

    You can also set its value to "all", in order to enable all features in the project.

    Methods for setting these vary according to the IDE you are using - for example, if using VS Code, you can set it via the "Extension Settings" for Rust Analyzer.

    This assumes you are asking how to activate the features within Rust Analyzer - to activate them when building or running from Cargo, just use the --features option.

    See: Rust Analyzer Manual