Quick Ref: Cargo Commands ​
Updated Mar 2026Most-Used Commands ​
Command Reference ​
Project Management ​
| Command | Description |
|---|---|
cargo new my_app | Create binary project |
cargo new my_lib --lib | Create library project |
cargo init | Initialize in current directory |
cargo init --lib | Initialize library in current directory |
Building ​
| Command | Description |
|---|---|
cargo build | Debug build (target/debug/) |
cargo build --release | Release build (target/release/) |
cargo build --target x86_64-unknown-linux-musl | Cross-compile |
cargo check | Fast check without generating binary |
cargo clean | Remove target/ directory |
Running ​
| Command | Description |
|---|---|
cargo run | Build and run |
cargo run -- arg1 arg2 | Run with arguments |
cargo run --release | Run optimized build |
cargo run --bin my_binary | Run specific binary |
cargo run --example basic | Run an example |
Testing ​
| Command | Description |
|---|---|
cargo test | Run all tests |
cargo test test_name | Run matching tests |
cargo test --lib | Unit tests only |
cargo test --doc | Doc tests only |
cargo test --test integration | Specific integration test |
cargo test -- --nocapture | Show println output |
cargo test -- --test-threads=1 | Run serially |
cargo test -- --ignored | Run ignored tests |
Dependencies ​
| Command | Description |
|---|---|
cargo add serde | Add dependency |
cargo add serde --features derive | Add with features |
cargo add --dev pretty_assertions | Add dev dependency |
cargo add --build cc | Add build dependency |
cargo remove serde | Remove dependency |
cargo update | Update all deps (within semver) |
cargo update serde | Update specific dep |
cargo tree | Show dependency tree |
cargo tree -d | Show duplicate dependencies |
Code Quality ​
| Command | Description |
|---|---|
cargo clippy | Run linter |
cargo clippy -- -D warnings | Lint, treat warnings as errors |
cargo clippy --fix | Auto-fix lints |
cargo fmt | Format code |
cargo fmt --check | Check formatting without changing |
Documentation ​
| Command | Description |
|---|---|
cargo doc | Generate documentation |
cargo doc --open | Generate and open in browser |
cargo doc --no-deps | Skip dependency docs |
Publishing ​
| Command | Description |
|---|---|
cargo login | Authenticate with crates.io |
cargo package | Create .crate archive |
cargo package --list | List files that will be included |
cargo publish | Publish to crates.io |
cargo publish --dry-run | Test publish without uploading |
cargo yank --version 0.1.0 | Yank a published version |
Benchmarks ​
| Command | Description |
|---|---|
cargo bench | Run benchmarks |
cargo bench -- bench_name | Run specific benchmark |
Workspace Operations ​
| Command | Description |
|---|---|
cargo build --workspace | Build all crates |
cargo test --workspace | Test all crates |
cargo build -p my_crate | Build specific crate |
cargo test -p my_crate | Test specific crate |
Essential Plugins ​
bash
cargo install cargo-watch # Auto-rebuild
cargo install cargo-expand # Expand macros
cargo install cargo-bloat # Binary size analysis
cargo install cargo-outdated # Find outdated deps
cargo install cargo-audit # Security audit
cargo install cargo-deny # License + advisory checks
cargo install cargo-flamegraph # Profiling
cargo install cargo-tarpaulin # Code coveragePlugin Usage ​
| Command | Description |
|---|---|
cargo watch -x check | Re-check on save |
cargo watch -x 'test -- --nocapture' | Re-test on save |
cargo expand | Show expanded macros |
cargo bloat --release --crates | Size by crate |
cargo outdated | Show outdated deps |
cargo audit | Check vulnerabilities |
cargo tarpaulin --out html | Code coverage report |
Environment Variables ​
| Variable | Description |
|---|---|
RUST_LOG=debug | Set log level (with env_logger) |
RUST_BACKTRACE=1 | Show backtrace on panic |
RUST_BACKTRACE=full | Full backtrace with line numbers |
CARGO_HOME | Override Cargo home directory |
RUSTFLAGS="-D warnings" | Pass flags to rustc |