Cargo features are named flags in Cargo.toml that enable optional compilation
Answer: Cargo features are named flags you define in the [features] table of Cargo.toml. Each feature is either enabled or disabled. Features of the package being built can be turned on with command-line flags such as --features. Features of dependencies are enabled in the dependency declaration.
What a feature enables
A feature lists other features or optional dependencies that it turns on. In code, gate a module with #[cfg(feature = "webp")]. Cargo passes these to rustc with --cfg, and you can test them with the cfg attribute or the cfg macro.
Building the package turns on the default feature, which then enables the features it lists. Use the --no-default-features command-line flag to disable the package's default features. In a dependency declaration, set default-features = false. Dependencies turn default features on automatically unless that option is set.
See also
Claims
- id: C1 claim: Features are defined in the [features] table of Cargo.toml. source: S1 evidence: "named features in the [features] table of Cargo.toml"
- id: C2 claim: Package features can be enabled on the command line with --features. source: S1 evidence: "enabled on the command-line with flags such as --features"
- id: C3 claim: A feature lists other features or optional dependencies it enables. source: S2 evidence: "other features or optional dependencies that it enables"
- id: C4 claim: The default feature is enabled when the package is built. source: S2 evidence: "the
defaultfeature is enabled which in turn"
Sources
- [S1] Features. The Cargo Book. https://doc.rust-lang.org/cargo/reference/features.html . License: MIT OR Apache-2.0. Accessed 2026-11-17.
- [S2] features.md (The Cargo Book source). rust-lang/cargo (GitHub). https://github.com/rust-lang/cargo/blob/master/doc/book/src/reference/features.md . License: MIT OR Apache-2.0. Accessed 2026-11-17.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: The Cargo Book, Features, MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/cargo/master/LICENSE-MIT, https://raw.githubusercontent.com/rust-lang/cargo/master/LICENSE-APACHE). Copyright (c) The Rust Project Developers; rust-lang/cargo (GitHub), features.md (The Cargo Book source), MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/cargo/master/LICENSE-MIT, https://raw.githubusercontent.com/rust-lang/cargo/master/LICENSE-APACHE). Copyright (c) The Rust Project Developers. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: The Cargo Book, Features, MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/cargo/master/LICENSE-MIT, https://raw.githubusercontent.com/rust-lang/cargo/master/LICENSE-APACHE). Copyright (c) The Rust Project Developers; rust-lang/cargo (GitHub), features.md (The Cargo Book source), MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/cargo/master/LICENSE-MIT, https://raw.githubusercontent.com/rust-lang/cargo/master/LICENSE-APACHE). Copyright (c) The Rust Project Developers.
Cite this
iinventedlight. 2026. "Cargo features are named flags in Cargo.toml that enable optional compilation." iinventedlight Library, iil:note:hpwe5kjg, version of 2026-11-17. https://iinventedlight.com/library/notes/rust-cargo-features/. License: CC-BY-4.0.
@misc{iil_note_hpwe5kjg,
author = {{iinventedlight}},
title = {{Cargo features are named flags in Cargo.toml that enable optional compilation}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-17},
url = {https://iinventedlight.com/library/notes/rust-cargo-features/},
note = {Permanent ID iil:note:hpwe5kjg. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- rust-cargo-features.table.json (table; CC-BY-4.0)
- rust-cargo-features.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/rust-cargo-features.md.