liveBook · Manning

Cargo feature

As explained in Refactoring to Rust

A Cargo feature is a named configuration option that conditionally enables code paths, dependencies, or capabilities in a Rust crate.

A Cargo feature is a named configuration option for conditional compilation. It can select code paths, add dependencies, or enable features in dependencies. section 6.3

Use features to add optional capabilities without enabling every dependency behavior:

  • Add a feature in a dependency configuration object. section 6.3
  • Enable Serde's derive feature to generate parsing and deserialization implementations for a data type. section 6.3
  • Combine the feature with a derived trait such as serde::Deserialize to make JSON parsing work. section 6.3

Enabling Serde derive support

In Cargo.toml, write `serde = { version = "1.0", features = ["derive"] }`. Then `#[derive(Debug, serde::Deserialize)]` lets `serde_json::from_str` deserialize JSON into a declared Rust struct. section 6.3

How Refactoring to Rust covers it

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 it assumes
Assumes basic Cargo dependencies, Rust structs, derive attributes, and trait-based JSON deserialization.
What you can do after
Apply it via Cargo to enable dependency capabilities and deserialize JSON into a typed Rust struct.
How it connects
Connects conditional compilation, dependencies, Serde, derive macros, traits, and JSON parsing.

Also explained in

Compare how 2 books explain cargo feature →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Refactoring to Rust. The section links point to the parts of the book it draws on.