Sub topic 7
Enabling and disabling crate features
This chapter assumes you can edit Cargo.toml, run cargo add, and read SemVer requirements such as "1" and "~1.2.3". It covers how to turn a crate's optional parts on and off.
What a feature is
A feature is a named switch that a crate author defines. Each switch controls optional code or optional dependencies. Features keep compile times and binary sizes down, because you build only the parts you use.
Features are additive. Enabling a feature must never remove functionality, and Cargo relies on this when it combines requests from several crates.
A crate can also list a set of default features. Cargo enables these unless you opt out.
Enabling features
Add a feature with the features key. The serde crate hides its derive macros behind a feature called derive:
[dependencies]
serde = { version = "1", features = ["derive"] }Without derive, use serde::Serialize; still imports the trait, but #[derive(Serialize)] fails to compile. The macro does not exist until the feature is on.
The command line does the same edit for you:
cargo add serde --features deriveDisabling default features
Many crates turn on features you may not need. Set default-features = false to opt out, then list only what you want:
[dependencies]
tokio = { version = "1", default-features = false, features = ["rt", "macros"] }On the command line, use --no-default-features:
cargo add tokio --no-default-features --features rt,macrosCheck the crate's documentation on docs.rs before doing this. The feature list there shows which features exist and what each one enables.
How Cargo combines features
If two crates in your tree depend on the same package, Cargo builds that package once. It takes the union of all requested features. This process is called feature unification.
The diagram below shows the effect.
A consequence is that a dependency may be built with more features than your own Cargo.toml asks for. Another crate in the tree can switch them on. Setting default-features = false only helps if every crate that depends on the package does the same.
Common errors
- A derive or function is missing. The compiler reports that a macro or item cannot be found. Check whether the item is gated behind a feature that you have not enabled.
- A feature name is rejected. Cargo reports that the package does not have that feature. Feature names are case-sensitive, so compare yours with the crate's
[features]table on docs.rs. - Disabling defaults breaks the build. The crate's defaults may include something other features need. Add back the features you lose one at a time until it compiles.
- A feature appears enabled that you never asked for. Another dependency requested it. See the next section.
Seeing which features are active
Use cargo tree with the features edge type to see the result after unification:
cargo tree -e features
cargo tree -e features -i tokioThe first command lists each package with the features enabled on it. The second, with -i, inverts the tree for tokio and shows which packages requested which feature.
Exercise
Your project depends on serde = "1" and the code #[derive(Serialize)] fails to compile with "cannot find derive macro". What is the fix, and what command applies it?
Show answer
The derive macros live behind the derive feature. Enable it by changing the entry to serde = { version = "1", features = ["derive"] }, or run cargo add serde --features derive.
Exercise
You set default-features = false on a crate, but cargo tree -e features still shows a default feature enabled. Give one likely reason.
Show answer
Another package in the dependency tree depends on the same crate with its default features on. Cargo unifies features by taking the union, so the feature is enabled for everyone. cargo tree -e features -i <crate> shows which package requested it.