Skip to content

Quick Ref: Cargo Commands ​

Updated Mar 2026

Most-Used Commands ​

Command Reference ​

Project Management ​

CommandDescription
cargo new my_appCreate binary project
cargo new my_lib --libCreate library project
cargo initInitialize in current directory
cargo init --libInitialize library in current directory

Building ​

CommandDescription
cargo buildDebug build (target/debug/)
cargo build --releaseRelease build (target/release/)
cargo build --target x86_64-unknown-linux-muslCross-compile
cargo checkFast check without generating binary
cargo cleanRemove target/ directory

Running ​

CommandDescription
cargo runBuild and run
cargo run -- arg1 arg2Run with arguments
cargo run --releaseRun optimized build
cargo run --bin my_binaryRun specific binary
cargo run --example basicRun an example

Testing ​

CommandDescription
cargo testRun all tests
cargo test test_nameRun matching tests
cargo test --libUnit tests only
cargo test --docDoc tests only
cargo test --test integrationSpecific integration test
cargo test -- --nocaptureShow println output
cargo test -- --test-threads=1Run serially
cargo test -- --ignoredRun ignored tests

Dependencies ​

CommandDescription
cargo add serdeAdd dependency
cargo add serde --features deriveAdd with features
cargo add --dev pretty_assertionsAdd dev dependency
cargo add --build ccAdd build dependency
cargo remove serdeRemove dependency
cargo updateUpdate all deps (within semver)
cargo update serdeUpdate specific dep
cargo treeShow dependency tree
cargo tree -dShow duplicate dependencies

Code Quality ​

CommandDescription
cargo clippyRun linter
cargo clippy -- -D warningsLint, treat warnings as errors
cargo clippy --fixAuto-fix lints
cargo fmtFormat code
cargo fmt --checkCheck formatting without changing

Documentation ​

CommandDescription
cargo docGenerate documentation
cargo doc --openGenerate and open in browser
cargo doc --no-depsSkip dependency docs

Publishing ​

CommandDescription
cargo loginAuthenticate with crates.io
cargo packageCreate .crate archive
cargo package --listList files that will be included
cargo publishPublish to crates.io
cargo publish --dry-runTest publish without uploading
cargo yank --version 0.1.0Yank a published version

Benchmarks ​

CommandDescription
cargo benchRun benchmarks
cargo bench -- bench_nameRun specific benchmark

Workspace Operations ​

CommandDescription
cargo build --workspaceBuild all crates
cargo test --workspaceTest all crates
cargo build -p my_crateBuild specific crate
cargo test -p my_crateTest 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 coverage

Plugin Usage ​

CommandDescription
cargo watch -x checkRe-check on save
cargo watch -x 'test -- --nocapture'Re-test on save
cargo expandShow expanded macros
cargo bloat --release --cratesSize by crate
cargo outdatedShow outdated deps
cargo auditCheck vulnerabilities
cargo tarpaulin --out htmlCode coverage report

Environment Variables ​

VariableDescription
RUST_LOG=debugSet log level (with env_logger)
RUST_BACKTRACE=1Show backtrace on panic
RUST_BACKTRACE=fullFull backtrace with line numbers
CARGO_HOMEOverride Cargo home directory
RUSTFLAGS="-D warnings"Pass flags to rustc

Built with VitePress | Rust SOP Documentation