rgoussu@goussu: ~/library/rust/toolchain
~/library/rust/toolchain cat cargo.md

cargo — the front door

# The daily interface to everything — build/run/test/bench/doc, dependency management with cargo add, the cargo-* plugin convention, and what each command actually does.

Conceptsaved 2026-08-09 #rust#tooling#cargo#build

Overview

cargo is the only interface a Rust developer uses day to day: it drives rustc with the right flags, resolves and fetches dependencies from crates.io, runs tests and benchmarks, invokes rustfmt and clippy, and dispatches any cargo-* binary as a subcommand. This note covers the daily surface; the project-model machinery — workspaces, features, profiles, build.rs, publishing — has its own note in the build group: Cargo in depth.

Key points

  • The core loop: cargo check (type-check without codegen — the fast feedback command, and what rust-analyzer runs), cargo build (debug by default), cargo run, cargo test, cargo build --release when you actually need speed.
  • cargo check is the habit that matters: an order of magnitude faster than a full build because it stops before LLVM; the edit-check loop is where Rust development actually happens.
  • Dependency edits are commands, not file surgery: cargo add serde --features derive, cargo remove, cargo update (respecting semver ranges), cargo tree to see the resolved graph and cargo tree -d to find duplicate versions.
  • Quality commands front the components: cargo fmt and cargo clippy run the rustup-installed rustfmt and clippy; cargo doc --open runs rustdoc.
  • The plugin convention: any binary named cargo-foo on PATH is callable as cargo foo; cargo install puts binaries in ~/.cargo/bin. The entire ecosystem of nextest, expand, audit, deny, dist rides on this.
  • Target selection: --bin, --lib, --example, --test, --bench pick which target of the package to build; cargo run --example demo is how examples double as runnable docs.
  • Output lives in target/: shared per workspace, keyed by profile; it grows without bound — cargo clean, or cargo-sweep/sccache on CI.
  • Offline & locked: --locked (fail if Cargo.lock would change — use in CI) and --offline make builds reproducible and network-free.

Examples

cargo new my-tool --bin              # scaffold: src/main.rs + Cargo.toml
cargo check                          # the fast loop
cargo add axum tokio --features tokio/full
cargo tree -d                        # which deps pull two versions of the same crate?
cargo test -- --nocapture            # let println! through
cargo build --release --locked       # CI build

Related