cargo commands cheatsheet.
Create, build, run and test — the Cargo commands you use every day in Rust. Tap to copy.
cargo new <name>Create a new project in a new directorycargo initCreate a project in the current directorycargo buildCompile the project (debug build)cargo build --releaseCompile with optimizations for releasecargo runBuild and run the projectcargo run -- <args>Run the project passing arguments to itcargo add <crate>Add a dependency to Cargo.tomlcargo add <crate>@<version>Add a dependency at a specific versioncargo remove <crate>Remove a dependency from Cargo.tomlcargo updateUpdate dependencies in Cargo.lockcargo testRun all testscargo test <name>Run tests matching a namecargo checkCheck for errors without building a binarycargo benchRun benchmarkscargo fmtFormat the code with rustfmtcargo clippyLint the code for common mistakescargo doc --openBuild the docs and open them in a browsercargo treeShow the dependency treecargo cleanRemove the target build directorycargo search <query>Search crates.io for a packagecargo --versionShow the installed Cargo versionWhat 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.