๐Ÿ“‹ Cheat Sheets

Cargo Cheat Sheet โ€” Build, Test, and Manage Rust Projects


Click any item to expand the explanation and examples.

๐Ÿš€ Create & Run

cargo new / cargo init create
cargo new my-app              # New project with directory
cargo new my-lib --lib        # New library
cargo init                    # Init in current directory
cargo init --lib              # Init as library
cargo run / cargo build build
cargo run                     # Build + run
cargo run -- --port 8080      # Pass args to your program
cargo build                   # Debug build
cargo build --release         # Optimized release build
cargo check                   # Check for errors (faster than build)
cargo clean                   # Remove target/ directory

๐Ÿ“ฆ Dependencies

cargo add / Cargo.toml deps
cargo add serde                       # Add dependency
cargo add serde --features derive     # With features
cargo add tokio -F full               # Shorthand for features
cargo add --dev mockall               # Dev dependency
cargo remove serde                    # Remove dependency
cargo update                          # Update to latest allowed versions
cargo update -p serde                 # Update specific package

Cargo.toml:

[dependencies]
serde = { version = "1.0", features = ["derive"] }
tokio = { version = "1", features = ["full"] }
reqwest = { version = "0.12", features = ["json"] }

[dev-dependencies]
mockall = "0.13"

๐Ÿงช Test & Lint

cargo test test
cargo test                    # Run all tests
cargo test my_test            # Run tests matching name
cargo test -- --nocapture     # Show println! output
cargo test -- --test-threads=1  # Run tests sequentially
cargo test --doc              # Run doc tests only
cargo fmt / cargo clippy lint
cargo fmt                     # Format code
cargo fmt -- --check          # Check formatting (CI)
cargo clippy                  # Lint (catches common mistakes)
cargo clippy -- -D warnings   # Treat warnings as errors

๐Ÿ“– Docs & Publish

cargo doc / cargo publish publish
cargo doc --open              # Generate + open docs in browser
cargo doc --no-deps           # Only your crate's docs
cargo publish                 # Publish to crates.io
cargo publish --dry-run       # Test publish without uploading
cargo login                   # Authenticate with crates.io

See also: Rust cheat sheet