Skip to content

/cheatsheets/cargo/cargo-cheat-sheet

Cargo cheat sheet: build, run, test and manage Rust crates

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · cargo 1.98.1 (797e8a9bc 2026-08-05)updated
tool
Cargo
versions
Cargo 1.62+ (cargo add)
os
linux, macos, windows

Cargo is Rust's build tool and package manager. The commands below cover a typical day: cargo new to start, cargo check/build/run while coding, cargo test before committing, and cargo add/update for dependencies.

A typical session

bash
cargo new -q --vcs none hello && cd hello
cat >> src/main.rs <<'EOF'

fn add(a: i32, b: i32) -> i32 { a + b }

#[cfg(test)]
mod tests {
    #[test]
    fn adds() { assert_eq!(super::add(2, 2), 4); }
}
EOF
cargo run -q
cargo test -q 2>&1 | sed -E 's/finished in [0-9.]+s/finished in <time>/'
ls target

Output

warning: function `add` is never used
 --> src\main.rs:5:4
  |
5 | fn add(a: i32, b: i32) -> i32 { a + b }
  |    ^^^
  |
  = note: `#[warn(dead_code)]` (part of `#[warn(unused)]`) on by default

Hello, world!

running 1 test
.
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in <time>

CACHEDIR.TAG
debug

Create

CommandEffect
cargo new NAMENew binary package in folder NAME (with a git repository)
cargo new --lib NAMENew library package
cargo initTurn the current folder into a package
cargo new --vcs none NAMENo git repository

Build and run

CommandEffect
cargo checkType-check without producing a binary (fastest feedback)
cargo buildDebug build in target/debug
cargo build --releaseOptimised build in target/release
cargo run -- ARGSBuild and run; ARGS after -- go to the program
cargo run --bin NAMERun one of several binaries
cargo run --example NAMERun examples/NAME.rs
cargo cleanDelete the target folder

Test, lint, format, docs

CommandEffect
cargo testRun unit, integration and doc tests
cargo test NAMEOnly tests whose name contains NAME
cargo test -- --nocaptureShow println! output of passing tests
cargo fmtFormat the code with rustfmt
cargo fmt --checkFail if formatting would change files (CI)
cargo clippyLints from Clippy
cargo clippy -- -D warningsTreat lint warnings as errors (CI)
cargo doc --openBuild the documentation and open it

Dependencies

CommandEffect
cargo add serde --features deriveAdd a dependency to Cargo.toml (Cargo 1.62+)
cargo add --dev pretty_assertionsAdd a dev-dependency
cargo remove serdeRemove a dependency
cargo updateUpdate Cargo.lock within the version requirements
cargo update -p serdeUpdate one package only
cargo treeShow the dependency tree
cargo tree -i serdeShow what depends on serde
cargo install ripgrepInstall a binary crate into ~/.cargo/bin

Workspaces and selection

CommandEffect
cargo build -p NAMEBuild one member of a workspace
cargo test --workspaceTest every member
cargo build --manifest-path path/Cargo.tomlUse a manifest outside the current folder
cargo build --target x86_64-unknown-linux-muslCross-compile for another target (install it with rustup target add)
cargo build --lockedFail if Cargo.lock would change (CI)

Sources