Cargo Commands

cargo commands cheatsheet.

Create, build, run and test — the Cargo commands you use every day in Rust. Tap to copy.

Project
cargo new <name>Create a new project in a new directory
cargo initCreate a project in the current directory
cargo buildCompile the project (debug build)
cargo build --releaseCompile with optimizations for release
cargo runBuild and run the project
cargo run -- <args>Run the project passing arguments to it
Dependencies
cargo add <crate>Add a dependency to Cargo.toml
cargo add <crate>@<version>Add a dependency at a specific version
cargo remove <crate>Remove a dependency from Cargo.toml
cargo updateUpdate dependencies in Cargo.lock
Test & check
cargo testRun all tests
cargo test <name>Run tests matching a name
cargo checkCheck for errors without building a binary
cargo benchRun benchmarks
Quality
cargo fmtFormat the code with rustfmt
cargo clippyLint the code for common mistakes
cargo doc --openBuild the docs and open them in a browser
Info & maintenance
cargo treeShow the dependency tree
cargo cleanRemove the target build directory
cargo search <query>Search crates.io for a package
cargo --versionShow the installed Cargo version

What Cargo actually does

Cargo is the build tool and package manager that ships with Rust, and almost every Rust project leans on it for two jobs: pulling in crates other people wrote, and compiling and running your own code. The file at the centre of it all is Cargo.toml, which lists your dependencies and project metadata. When you clone a project, a single cargo build reads that file, resolves every crate, and compiles everything into a target folder — the first command you'll run on almost any codebase.

Creating and building

Starting out has a couple of flavours worth understanding. cargo new <name> scaffolds a fresh project in a new directory, while cargo init turns the current directory into one. Day to day you'll lean on cargo run, which builds and runs in one step, and cargo check, which type-checks your code without producing a binary — much faster when you just want to know it compiles. When you're ready to ship, cargo build --release turns on optimizations.

Crates and dependencies

The dependencies section of Cargo.toml is where crates live, and you rarely edit it by hand: cargo add <crate> writes the entry for you (add @<version> to pin one), and cargo remove <crate> takes it back out. cargo update moves your locked versions forward within the ranges you allow, and cargo tree shows exactly how everything fits together when a transitive dependency surprises you.

Keeping things healthy

A few habits keep a Rust project tidy. cargo fmt formats your code to the community style, cargo clippy catches common mistakes and non-idiomatic patterns, and cargo test runs your test suite. If builds start behaving strangely, cargo clean wipes the target directory for a fresh compile. These pair naturally with the Git commands for version control and the Docker commands when you containerise your app.

FAQ

What's the difference between cargo build and cargo check?
cargo check analyses your code for errors without producing a runnable binary, so it's much faster; cargo build actually compiles and links an executable you can run.
How do I build an optimized release?
Run cargo build --release to compile with optimizations into target/release — slower to build but far faster to run than the default debug build.

More cheatsheets