Cheatsheet: Cargo (Rust)

Last updated 2026-09-25

Basic Commands

Create a new Rust project

cargo new <project_name>

Build a project

cargo build

Build an optimized release binary

cargo build --release

Run a project

cargo run

Clean up build artifacts

cargo clean

Check project for errors without building

cargo check

Dependency Management

Add a new dependency

cargo add <dependency_name>

Add a dependency pinned to a version

cargo add <dependency_name>@<version>

Add a dependency with specific feature flags

cargo add <dependency_name> --features <feature1>,<feature2>

Add a dependency for dev/test builds only

cargo add <dependency_name> --dev

Update all dependencies

cargo update

List outdated dependencies

cargo outdated

Remove a dependency

cargo remove <dependency_name>

Testing & Documentation

Run all tests

cargo test

Run only tests whose name matches a substring

cargo test <substring>

Run tests without capturing stdout/println! output

cargo test -- --nocapture

Run tests sequentially instead of in parallel

cargo test -- --test-threads=1

Run benchmarks

cargo bench

Generate documentation

cargo doc

Open generated documentation

cargo doc --open

Formatting, Linting & Workspaces

Format code with rustfmt

cargo fmt

Check formatting without writing changes

cargo fmt --check

Lint with Clippy

cargo clippy

Fail Clippy on any warning (useful in CI)

cargo clippy -- -D warnings

Build/test only one crate in a workspace

cargo build -p <crate_name>

Print the resolved dependency tree

cargo tree

Publishing & Versioning

Publish a crate to crates.io

cargo publish

Check a crate before publishing

cargo package

Bump version number

cargo version --bump <major|minor|patch>

Cargo Deny (Dependency Auditing)

Install cargo-deny

cargo install cargo-deny

Run cargo-deny check for security, licenses, and more

cargo deny check

Check for security advisories

cargo deny check advisories

Check for outdated dependencies

cargo deny check bans

Check for banned or incompatible licenses

cargo deny check licenses

Generate cargo-deny config file

cargo deny init

FAQ