# How do Cargo features work, and how do I make a dependency optional?
Source: https://ruststudio.org/community/t/116/how-do-cargo-features-work-and-how-do-i-make-a-dependency-optional
Author: Ruststudio Team (staff) | Published: 2026-12-05 | Updated: 2026-12-05 | Checked with: rustc 1.93.0 and cargo 1.93.0, Windows 11
Category: Help and questions | Kind: FAQ | Tags: cargo, features, tooling | Views: 0

## Short answer
A feature is a named switch in Cargo.toml that turns optional code and optional dependencies on or off at build time. Declare it under [features], mark the dependency optional, guard the code with cfg(feature = "name"), and enable it with --features name.

## Answer
## Answer
A Cargo feature is a name you define in `Cargo.toml`. Code and dependencies can depend on that name, and whoever builds the crate decides whether it is on. Nothing is compiled for a feature that is off, so an optional dependency costs nothing when its feature is not enabled.

## How to do it
1. Mark the dependency `optional = true` and give it a feature. The `dep:` prefix keeps the dependency's own name from becoming a second, implicit feature.

```toml title=Cargo.toml
[package]
name = "feat"
version = "0.1.0"
edition = "2024"

[dependencies]
serde_json = { version = "1", optional = true }

[features]
default = []
json = ["dep:serde_json"]
```

2. Guard the code that needs it with `#[cfg(feature = "...")]`, or ask with `cfg!(feature = "...")`.

```rust title=src/main.rs
fn describe(items: &[(&str, u32)]) -> String {
    #[cfg(feature = "json")]
    {
        serde_json::to_string(items).unwrap()
    }
    #[cfg(not(feature = "json"))]
    {
        items.iter().map(|(n, c)| format!("{n}={c}")).collect::<Vec<_>>().join(",")
    }
}

fn main() {
    println!("{}", describe(&[("apples", 3), ("pears", 5)]));
    println!("json feature on: {}", cfg!(feature = "json"));
}
```

3. Build or run with and without the feature.

```text
> cargo run -q
apples=3,pears=5
json feature on: false
> cargo run -q --features json
[["apples",3],["pears",5]]
json feature on: true
```

## Why it works
`--features json` makes Cargo pass `--cfg 'feature="json"'` to the compiler and build the optional dependency. Features are additive: if two crates in one build ask for different features of the same dependency, Cargo builds it once with all of them on. For that reason a feature should only add things, never change what existing code does.

## Common mistakes
- Making a feature that removes functionality (a `no-json` feature). Name the feature after what it adds.
- Forgetting `default = [...]`. Features listed there are on unless a user passes `--no-default-features`.
- Guarding code with `#[cfg(feature = "json")]` without declaring `json` under `[features]`. The compiler then reports an unexpected `cfg` condition.

## Related questions
### How do I turn on features of a dependency?
Write them in the dependency line: `serde = { version = "1", features = ["derive"] }`.

### How do I build with every feature on?
`cargo build --all-features`. Use it in tests so every combination of your code gets compiled at least once.

## In Ruststudio
Ruststudio runs `cargo` for you, so a `[features]` table in `Cargo.toml` works as described here. The commands in the Run menu build with the features Cargo selects by default; for a feature that is off by default, run the command from the Terminal with `--features`.
