← All notes

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

Sources

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

Permanent ID: iil:note:hpwe5kjg

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.},
}

Markdown

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.

All artifact files (JSON index)

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.