Skip to main content

How to Add a Crate with Features with cargo add

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

Many Rust crates keep parts of themselves switched off until you ask for them. These optional parts are called features, and turning one on is part of adding the crate: cargo add <crate> --features <feature> writes both into Cargo.toml in one step.

Syntax​

Syntax
cargo add <crate>[@<version>] [--features <feature>[,<feature>]]
PartWhat it does
cargo addAdds a dependency to Cargo.toml in the project you're in.
<crate>The crate's name on crates.io, Rust's package registry, such as uuid.
@<version>Optional. A version, such as @1.29. Without it, Cargo picks the newest.
--features <feature>Optional. Turns on one or more optional features, separated by commas.

The Example​

The example program prints a new random UUID, a 36-character unique identifier, with the uuid crate. Random UUIDs are "version 4" UUIDs, and the uuid crate only includes the code that makes them when its v4 feature is on.

Prerequisites​

  • Rust, installed with rustup, which comes with Cargo. This guide uses Cargo 1.100.0.

Step 1: Create the Project​

Terminal
cargo new id-rust
cd id-rust

How to Create a Rust Project with cargo new explains the first command. The commands here are the same in zsh, bash and PowerShell.

Step 2: Add the Crate with Its Feature​

Terminal
cargo add uuid --features v4
Output
Updating crates.io index
Adding uuid v1.29.3 to dependencies
Features:
+ rng
+ std
+ v4
- arbitrary
- borsh
- bytemuck
- fast-rng
- js
- macro-diagnostics
- md5
- rng-getrandom
- rng-rand
- serde
- sha1
- slog
- uuid-rng-internal-lib
- v1
- v3
- v5
- v6
- v7
- v8
- zerocopy
Updating crates.io index
Locking 21 packages to highest Rust 1.100.0 compatible versions

The list shows every feature the crate offers. + means on and - means off. v4 is on because you asked for it, std is on by default, and rng came on because v4 needs random numbers. Everything else stays out of your program.

Cargo.toml now records the crate and the feature:

Cargo.toml
[package]
name = "id-rust"
version = "0.1.0"
edition = "2024"

[dependencies]
uuid = { version = "1.29.3", features = ["v4"] }

Step 3: Use the Feature​

Replace src/main.rs with:

src/main.rs, which imports uuid::Uuid and prints Uuid::new_v4().

src/main.rs
use uuid::Uuid;

fn main() {
println!("{}", Uuid::new_v4());
}
Terminal
cargo run -q
Output
247ffeb6-a10d-4fd6-ae81-af88e4b7fb37

A zsh terminal where cargo add uuid --features v4 adds uuid 1.29.3 with rng, std and v4 on and the other features off, and cargo run -q prints a random UUID.

Your UUID will differ: it's random every time.

What Happens Without the Feature​

Added with a plain cargo add uuid, the crate has no v4 code, and the same program doesn't compile:

Output of cargo build
error[E0599]: no associated function or constant named `new_v4` found for struct `Uuid` in the current scope

The function exists in the crate's documentation, but only when the feature is on. When the compiler says a function you can see in the docs doesn't exist, check the features.

Common Mistakes​

MistakeWhat happensDo this instead
Adding a crate without the feature its docs mentionFunctions from the documentation don't exist, as aboveAdd the feature with --features
Writing --features v4 v7 with a spaceCargo reads v7 as a second crate and stops: feature `v4` must be qualified by the dependency it's being activated forSeparate features with commas: --features v4,v7
Turning on every feature to be safeThe program compiles more code and pulls in more crates than it needsTurn on only what you use

Troubleshooting​

SymptomLikely causeFix
error[E0599]: no associated function or constant named `new_v4` foundThe v4 feature isn't oncargo add uuid --features v4
A feature name isn't acceptedThe crate has no feature by that nameCheck the list cargo add prints, or the crate's documentation

FAQ​

How do I turn a feature on for a crate I've already added?​

Run cargo add again with the feature, such as cargo add uuid --features v4. Cargo updates the existing line in Cargo.toml.

How do I find a crate's features?​

cargo add prints them, as above, and each crate's page on docs.rs lists them under "Feature flags".

Conclusion​

cargo add <crate> --features <feature> adds a crate and turns on the optional parts you need, and Cargo.toml records both. When a function from the docs is missing, the feature that provides it is usually off.

Additional Resources​