Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Appendix A — Cargo Command Cheat Sheet

Command Board

Cargo Surfaces by Workflow Phase

developcheck, build, run, fmttesttest, bench, docpublishpackage, publish, yankinspecttree, expand, miriworkspace-p, --workspace, audit

Development

CommandWhat it doesWhen to use it
cargo new appCreate a new binary crateStarting a CLI or service
cargo new --lib crate_nameCreate a new library crateStarting reusable code
cargo initTurn an existing directory into a Cargo projectBootstrapping inside an existing repo
cargo checkType-check and borrow-check without final codegenYour default inner loop
cargo buildCompile a debug buildWhen you need an actual binary
cargo build --releaseCompile with optimizationsBenchmarking, shipping, profiling
cargo runBuild and run the default binaryQuick execution during development
cargo run -- arg1 arg2Pass CLI arguments after --Testing CLI paths
cargo cleanRemove build artifactsResolving stale artifact confusion
cargo fmtFormat the workspaceBefore commit
cargo fmt --checkCheck formatting without changing filesCI
cargo clippyRun lintsBefore every PR
cargo clippy -- -D warningsTreat all warnings as errorsCI and strict local checks

Testing

CommandWhat it doesWhen to use it
cargo testRun unit, integration, and doc testsGeneral verification
cargo test name_filterRun tests matching a substringNarrowing failures
cargo test -- --nocaptureShow test stdout/stderrDebugging test output
cargo test --test apiRun one integration test targetLarge repos with many test files
cargo test -p crate_nameTest one workspace memberFaster workspace iteration
cargo test --features fooTest feature-specific behaviorVerifying gated code
cargo test --all-featuresTest with all features enabledRelease validation
cargo test --no-default-featuresTest minimal feature setLibrary compatibility work
cargo benchRun benchmarks (when configured)Performance comparison
cargo doc --openBuild docs and open them locallyReviewing public API docs
cargo test --docRun doctests onlyAPI doc verification

Publishing and Dependency Management

CommandWhat it doesWhen to use it
cargo add serdeAdd a dependencyDay-to-day dependency management
cargo add tokio --features fullAdd a dependency with featuresAsync/service setup
cargo remove serdeRemove a dependencyPruning unused crates
cargo updateUpdate lockfile-resolved versionsRefreshing dependencies
cargo update -p serdeUpdate one package selectivelyTargeted dependency bump
cargo treeShow dependency treeAuditing transitive dependencies
cargo tree -dShow duplicates in dependency graphSize and compile-time cleanup
cargo publish --dry-runValidate crate packagingBefore release
cargo packageBuild the publishable tarballInspecting release contents
cargo yank --vers 1.2.3Yank a published versionPreventing new downloads of a bad release

Debugging and Inspection

CommandWhat it doesWhen to use it
rustc --explain E0382Explain an error code in detailReading compiler intent
cargo expandShow macro-expanded codeDerive/macros/debugging
cargo metadata --format-version 1Output machine-readable project metadataTooling and repo inspection
cargo locate-projectShow current manifest pathScripts/tooling
cargo bloatInspect binary size contributorsRelease-size investigation
cargo flamegraphGenerate a profiling flamegraphCPU performance work
cargo miri testInterpret code under MiriUndefined-behavior hunting
cargo llvm-linesInspect LLVM IR line growthMonomorphization/code-size analysis

Workspace and CI

CommandWhat it doesWhen to use it
cargo build --workspaceBuild all workspace membersCI and release checks
cargo test --workspaceTest the full workspaceCI and local full validation
cargo check -p crate_nameCheck one workspace memberFocused iteration
cargo build --features "foo,bar"Build specific feature combinationsCompatibility testing
cargo build --target x86_64-unknown-linux-muslCross-compileRelease engineering
cargo auditCheck for vulnerable dependenciesSecurity hygiene
cargo semver-checks check-releaseDetect public semver breaksLibrary release review

Practical Workflow

SituationBest first command
Editing logic in a cratecargo check
Finishing a change for reviewcargo fmt && cargo clippy -- -D warnings && cargo test
Debugging macro outputcargo expand
Inspecting repo structurecargo tree and cargo metadata
Verifying a library releasecargo test --all-features && cargo semver-checks check-release