How to activate an optional dependency?

Viewed 711

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?

2 Answers

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 rust-analyzer.cargo.allFeatures to true, 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

All non-default features need to be specified when running. So run with:

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

Check cargo run --help for more help.

Related