/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
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 targetOutput
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
debugCreate
| Command | Effect |
|---|---|
| cargo new NAME | New binary package in folder NAME (with a git repository) |
| cargo new --lib NAME | New library package |
| cargo init | Turn the current folder into a package |
| cargo new --vcs none NAME | No git repository |
Build and run
| Command | Effect |
|---|---|
| cargo check | Type-check without producing a binary (fastest feedback) |
| cargo build | Debug build in target/debug |
| cargo build --release | Optimised build in target/release |
| cargo run -- ARGS | Build and run; ARGS after -- go to the program |
| cargo run --bin NAME | Run one of several binaries |
| cargo run --example NAME | Run examples/NAME.rs |
| cargo clean | Delete the target folder |
Test, lint, format, docs
| Command | Effect |
|---|---|
| cargo test | Run unit, integration and doc tests |
| cargo test NAME | Only tests whose name contains NAME |
| cargo test -- --nocapture | Show println! output of passing tests |
| cargo fmt | Format the code with rustfmt |
| cargo fmt --check | Fail if formatting would change files (CI) |
| cargo clippy | Lints from Clippy |
| cargo clippy -- -D warnings | Treat lint warnings as errors (CI) |
| cargo doc --open | Build the documentation and open it |
Dependencies
| Command | Effect |
|---|---|
| cargo add serde --features derive | Add a dependency to Cargo.toml (Cargo 1.62+) |
| cargo add --dev pretty_assertions | Add a dev-dependency |
| cargo remove serde | Remove a dependency |
| cargo update | Update Cargo.lock within the version requirements |
| cargo update -p serde | Update one package only |
| cargo tree | Show the dependency tree |
| cargo tree -i serde | Show what depends on serde |
| cargo install ripgrep | Install a binary crate into ~/.cargo/bin |
Workspaces and selection
| Command | Effect |
|---|---|
| cargo build -p NAME | Build one member of a workspace |
| cargo test --workspace | Test every member |
| cargo build --manifest-path path/Cargo.toml | Use a manifest outside the current folder |
| cargo build --target x86_64-unknown-linux-musl | Cross-compile for another target (install it with rustup target add) |
| cargo build --locked | Fail if Cargo.lock would change (CI) |
Sources
Related pages
flags · cargocargo build --release: optimised builds and target/releasecargo build --release uses the release profile (opt-level 3, no overflow checks) and writes to target/release. Debug vs release, and how to tune the profile.errors · cargoerror: could not find `Cargo.toml` in `...` or any parent directorycargo reports could not find Cargo.toml when run outside a Rust package. cd into the project, pass --manifest-path, or create a package with cargo new.errors · rustRust error[E0382]: borrow of moved valuerustc reports E0382 when a variable is used after its value moved to another binding or function. Borrow it with &, clone it, or reorder the code. Real output.