Useful Cargo Subcommands
Beyond build and test, the ecosystem adds subcommands for macro expansion, dependency graphs, binary size analysis, faster tests, and auto-rebuild workflows.
Busca en todas las páginas de la documentación
Beyond build and test, the ecosystem adds subcommands for macro expansion, dependency graphs, binary size analysis, faster tests, and auto-rebuild workflows.
cargo install cargo-expand cargo-nextest cargo-watch cargo-bloat
cargo expand my_fn
cargo tree -d
cargo bloat --release
cargo nextest run
cargo watch -x checkWhen to reach for this: Debugging macros, slimming release binaries, or tightening the local dev loop.
Daily developer toolkit for a workspace:
# Type-check on save
cargo watch -c -x 'check --workspace'
# Expand a derive macro output
cargo expand -p api handlers::create_order
# Find duplicate deps
cargo tree -d
# Release binary size breakdown
cargo bloat --release -n 20
# Faster parallel test runner
cargo nextest run --workspaceWhat this demonstrates:
cargo-expand shows macro-generated code for debuggingcargo tree visualizes and deduplicates the dependency graphcargo bloat attributes binary size to symbols and cratescargo-nextest runs tests with better parallelism and reportingcargo-watch reruns commands when files change| Command | Purpose |
|---|---|
cargo check | Fast type-check without linking |
cargo doc --open | Build and browse docs |
cargo metadata | JSON project graph for tooling |
cargo package | Validate publish tarball |
cargo fmt / clippy | Format and lint (rustfmt/clippy) |
| Tool | Install | Use |
|---|---|---|
cargo-expand | cargo install cargo-expand | Inspect macro output |
cargo-bloat | cargo install cargo-bloat | Size profiling |
cargo-nextest | cargo install cargo-nextest | Fast test orchestration |
cargo-watch | cargo install cargo-watch | File-watch automation |
cargo-llvm-cov | cargo install cargo-llvm-cov | Coverage reports |
- run: cargo nextest run --workspace
- run: cargo clippy --workspace --all-targets -- -D warnings
- run: cargo bloat --release --crates | head -30-p crate_name in workspaces.--release - misleading sizes vs production. Fix: always profile release builds.test-threads = 1. Fix: .config/nextest.toml per crate needs.-c clear screen and debounce defaults.~/.cargo/bin or use prebuilt CI images.| Alternative | Use When | Don't Use When |
|---|---|---|
rustc -Zunpretty=expanded | Nightly-only macro debug | Stable toolchain teams |
Plain cargo test | Simple repos, few tests | Large workspaces needing nextest filters |
mold / lld linker | Link time pain | Macro/debug workflows |
cargo expand -p mycrate path::to::Type shows the full expansion including #[derive(serde::Deserialize)] output.
Inverse tree: shows which crates depend on a given package (useful for auditing why windows-sys appears).
Mostly yes; some custom test harnesses need configuration. nextest supports retries, partitions, and JUnit output.
cargo watch -x 'check -p api' scopes to a workspace member.
cargo bloat --release --crates aggregates by dependency crate name.
Install cargo-hack to check all feature combinations (cargo hack check --feature-powerset).
Use mold or lld via .cargo/config.toml linker settings; separate from subcommands but pairs well with watch/check loops.
Yes, after full macro expansion. For proc-macro crate debugging, also use eprintln! in the macro crate.
cargo --list shows built-in and third-party subcommands on PATH.
They run on your host toolchain; keep them updated alongside Rust 1.97. They do not affect published crate MSRV.
Stack versions: This page was written for Rust 1.97.0 (edition 2024), Tokio 1.x, Axum 0.8, serde 1.0, sqlx 0.8, clap 4, and Polars 0.46+.
Revisado por Chris St. John·Última actualización: 16 jul 2026