Cargo feature
A Cargo feature is a named compile-time option that conditionally enables functionality or dependencies in a Rust package.
Write Powerful Rust Macros
A Cargo feature is a named compile-time configuration option that conditionally enables additive crate functionality or dependencies.
- How it is used
- Makes optional macro functionality selectable so consumers pull in only the library code they need.
- How deep it goes
- Demonstrates Cargo.toml declaration, cfg-gated imports and modules, consumer activation, failure testing, and default-feature trade-offs.
- Examples
- Uses an optional config_struct attribute macro, Serde derive macros, and Tokio’s macros feature and tokio-macros dependency.
- What you can do after
- Operate/configure optional macro APIs by declaring features, guarding code with cfg, and enabling requested features in dependencies.
Read how Write Powerful Rust Macros explains cargo feature →
Embedded Software with Rust
A Cargo feature is a named compile-time option that conditionally enables code or dependencies in a Rust package.
- How it is used
- Tailors embedded firmware to hardware variants and debugging or production configurations.
- How deep it goes
- Explains feature declarations, optional dependencies, conditional compilation, build commands, and dependency-feature inspection.
- Examples
- Uses STM32 HAL selection, semihosting and panic backends, and a single-core critical-section feature.
- What you can do after
- Apply it via Cargo to configure dependencies, select conditional code, and inspect enabled features.
Read how Embedded Software with Rust explains cargo feature →
Refactoring to Rust
A Cargo feature is a named configuration option that conditionally enables code paths, dependencies, or capabilities in a Rust crate.
- How it is used
- Enables optional Rust capabilities such as Serde's generated parsing and deserialization support.
- How deep it goes
- Explains conditional compilation and demonstrates dependency configuration, but does not develop general feature design.
- Examples
- Uses Cargo.toml's serde derive feature and a Rust struct deriving Debug and Deserialize.
- What you can do after
- Apply it via Cargo to enable dependency capabilities and deserialize JSON into a typed Rust struct.
Related concepts
- additive featureincludes · An additive feature is a crate feature that only adds functionality and does not disable functionality already available.
- Cargopart of · Cargo is Rust’s package manager and build tool for managing projects and dependencies and for building, testing, and running Rust code.
- conditional compilationconfigured by · Conditional compilation selects source-code regions for inclusion or exclusion based on conditions evaluated during compilation or preprocessing.
- cargo.tomlpart of · Cargo.toml is a TOML-formatted manifest file that declares a Rust package’s metadata, dependencies, targets, and build configuration.
Questions about cargo feature
What is cargo feature?
A Cargo feature is a named compile-time option that conditionally enables functionality or dependencies in a Rust package.
Which Manning books explain cargo feature?
Write Powerful Rust Macros, Embedded Software with Rust and Refactoring to Rust.
What kinds of cargo feature are there?
Kinds of cargo feature include default feature and additive feature.
What is cargo feature part of?
Cargo feature is part of Cargo. Cargo is Rust’s package manager and build tool for managing projects and dependencies and for building, testing, and running Rust code.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.


