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.
Recipe
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.
Working Example
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-expandshows macro-generated code for debuggingcargo treevisualizes and deduplicates the dependency graphcargo bloatattributes binary size to symbols and cratescargo-nextestruns tests with better parallelism and reportingcargo-watchreruns commands when files change
Deep Dive
Built-in High-Value Commands
| 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) |
Installed Subcommands
| 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 |
CI Integration
- run: cargo nextest run --workspace
- run: cargo clippy --workspace --all-targets -- -D warnings
- run: cargo bloat --release --crates | head -30Gotchas
- Expand on wrong package - empty output. Fix: pass
-p crate_namein workspaces. - Bloat without
--release- misleading sizes vs production. Fix: always profile release builds. - nextest without config - some tests need
test-threads = 1. Fix:.config/nextest.tomlper crate needs. - watch spawning unbounded jobs - laptop fans spin. Fix: use
-cclear screen and debounce defaults. - Installing tools in CI every run - slow pipelines. Fix: cache
~/.cargo/binor use prebuilt CI images.
Alternatives
| 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 |
FAQs
How do I expand a specific derive?
cargo expand -p mycrate path::to::Type shows the full expansion including #[derive(serde::Deserialize)] output.
What does cargo tree -i do?
Inverse tree: shows which crates depend on a given package (useful for auditing why windows-sys appears).
Is nextest compatible with cargo test?
Mostly yes; some custom test harnesses need configuration. nextest supports retries, partitions, and JUnit output.
How do I watch only one crate?
cargo watch -x 'check -p api' scopes to a workspace member.
Can bloat show per-crate size?
cargo bloat --release --crates aggregates by dependency crate name.
What is cargo hack?
Install cargo-hack to check all feature combinations (cargo hack check --feature-powerset).
How do I speed up linking?
Use mold or lld via .cargo/config.toml linker settings; separate from subcommands but pairs well with watch/check loops.
Does expand work on procedural macros?
Yes, after full macro expansion. For proc-macro crate debugging, also use eprintln! in the macro crate.
How do I list installed subcommands?
cargo --list shows built-in and third-party subcommands on PATH.
Are these tools MSRV-sensitive?
They run on your host toolchain; keep them updated alongside Rust 1.97. They do not affect published crate MSRV.
Related
- Cargo Basics - core workflow
- Debugging Macros - macro troubleshooting
- Reducing Binary Size - act on bloat output
- Coverage & CI - llvm-cov in pipelines
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+.