Cargo Workflow · 18 min · free lesson

Feature Design

Make optional capability additive and explicit.

Core idea

Cargo features are unified across a dependency graph, so disabling one consumer's feature may not disable it globally.

Two dependents request different additive features and Cargo activates their union.
Features are unified across dependency paths.

Understand it

Cargo features are unified across a dependency graph, so disabling one consumer's feature may not disable it globally. Prefer additive capabilities and avoid features that make the same API mean incompatible things.

A feature is a named, additive capability a crate can enable at compile time. #[cfg(feature = "color")] includes code only when that feature is active; another branch can provide behavior without it. A feature is not a runtime switch, so a binary cannot toggle it after compilation. The public API exposed under different feature sets should be understandable and tested in each supported configuration.

Cargo unifies feature requests for the same package in a dependency graph. If one dependency path asks for feature A and another asks for feature B, the package may be built with both. That makes mutually exclusive features fragile: downstream users can accidentally activate the combination. Prefer features that add independent capabilities and keep a sensible default. If two backends truly cannot coexist, document and enforce that constraint explicitly, or consider a different design.

Create a tiny crate with a color feature and compile it with and without --features color. Then add two dependents that request different features of the same library and inspect the resulting feature graph with cargo tree -e features. Test --no-default-features if you claim that mode works. Avoid assuming that setting default-features = false at one edge globally disables defaults; another path can enable them. Feature design is part of the compatibility contract, not only a way to reduce compile time.

A feature name should describe the capability it adds. Avoid names that merely encode an internal implementation file, because external users will depend on the public meaning over time.

Read the code

#[cfg(feature = "color")]
fn paint() { println!("color"); }

#[cfg(not(feature = "color"))]
fn paint() { println!("plain"); }

Each build compiles exactly one paint, chosen by whether color is on.

Practice starter

#[cfg(feature = "backend_a")]
fn backend() -> &'static str { "a" }

#[cfg(feature = "backend_b")]
fn backend() -> &'static str { "b" }

fn main() {
    println!("{}", backend());
}

Practise this code in Playground ↗

Try it

Declare both features, build with --features backend_a,backend_b, and read the error. Then redesign them as additive capabilities that can be on at once.

What success looks like: Every feature combination compiles, including both on together, and you can say why Cargo may ask for that union.

Check your understanding

What style makes features compose well?

Reveal the answer
  1. Additive capability
  2. Mutual exclusion
  3. Hidden defaults

Additive capabilities only add code, so the union Cargo builds still compiles. Mutually exclusive features break as soon as two dependents enable both.

Go deeper

Common misconception: “Setting default-features = false on my dependency turns those features off everywhere.”

Cargo features are unified across a dependency graph, so disabling one consumer's feature may not disable it globally. Another path can still enable it.

  1. Read the gate: #[cfg(feature = "color")] keeps the first paint only when the color feature is on for this build.
  2. Read the fallback: #[cfg(not(feature = "color"))] provides the other paint, so exactly one definition exists in each build.
  3. Compare the builds: A call to paint() prints plain by default and color with --features color. The choice cannot change after compilation.

Transfer challenge: Add a fn shade() gated on a second feature, shadow, and list the four combinations with color. Predict what each one compiles, then check them with cargo check --features.

Connections

  • Dependency Kinds: Separate runtime, build, and test-only tools
  • Lockfile Reproducibility: Build the same resolved graph again
Learn this lesson with AI

Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.

I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools.
Lesson: Feature Design (courseId "projects", lessonId "feature-design")
Link: https://crabrix.com/learn/projects/feature-design/
Goal: Cargo features are unified across a dependency graph, so disabling one consumer's feature may not disable it globally.
Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=projects/feature-design) and I will write the code there.
Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.

In Claude Code, Codex or Cursor, paste it into a chat with Crabrix Academy connected. On a phone, the Claude and ChatGPT apps open the same links.