Toolkit: Cargo Plugins & Companions
Cargo is deliberately small, and a large ecosystem of tools fills the gaps. Most of them are Cargo subcommands: a program named cargo-foo on your PATH becomes cargo foo. This lesson shows how that mechanism works, then maps the tools you'll meet in real projects.
How cargo foo finds cargo-foo
There's no plugin API. When Cargo doesn't know a subcommand, it looks for an executable cargo-<name> in PATH (and ~/.cargo/bin) and runs it with the subcommand name as the first argument. Any language works:
mkdir -p ~/bin
cat > ~/bin/cargo-hello <<'SH'
#!/bin/bash
# Cargo calls us as: cargo-hello hello <args...>
shift
echo "cargo-hello: args = $*"
echo "cargo-hello: manifest = $(cargo locate-project --message-format plain 2>/dev/null || echo 'none here')"
SH
chmod +x ~/bin/cargo-hello
export PATH=$HOME/bin:$PATH
cargo hello --check fast
cargo --list | grep hello
cargo new demo --vcs none -q && cd demo
cargo hellocargo-hello: args = --check fast
cargo-hello: manifest = none here
hello
cargo-hello: args =
cargo-hello: manifest = /home/learner/demo/Cargo.toml
Rust tools are usually installed with cargo install <crate> (compiles from source into ~/.cargo/bin; add --locked to use the tool's own lockfile) or cargo-binstall (cargo binstall <crate> downloads a prebuilt binary when the project publishes one, which is much faster in CI).
How sccache plugs in: RUSTC_WRAPPER
Another extension point: if RUSTC_WRAPPER (or build.rustc-wrapper in config) is set, Cargo runs $RUSTC_WRAPPER rustc <args> instead of rustc <args>. That's all sccache needs to cache compilation results (locally or in S3/GCS/Redis, shared across CI machines). Here a tiny wrapper just logs what it sees:
mkdir -p ~/bin
cat > ~/bin/logwrap <<'SH'
#!/bin/bash
# $1 is the real rustc; the rest are its arguments
args=("$@")
for i in "${!args[@]}"; do
if [ "${args[$i]}" = "--crate-name" ]; then echo "wrapped rustc for: ${args[$((i+1))]}" >> ~/wrap.log; fi
done
exec "$@"
SH
chmod +x ~/bin/logwrap
cargo new app --vcs none -q && cd app
cargo add anyhow -q
RUSTC_WRAPPER=$HOME/bin/logwrap cargo build -q 2>&1 | grep -v Unpacking
cat ~/wrap.logwrapped rustc for: ___
wrapped rustc for: build_script_build
wrapped rustc for: anyhow
wrapped rustc for: app
Every compiler call went through the wrapper: Cargo's probe of the compiler (___), anyhow's build script, anyhow itself and the app. A caching wrapper hashes those arguments plus the input files and returns a stored result when it has one. Caching, distributed compilation and artifact management are the subject of Build Performance & Artifacts.
The map
| Need | Tool | Notes |
|---|---|---|
| edit dependencies | cargo add / cargo remove (built in since 1.62) |
from cargo-edit, which still adds cargo upgrade (bump requirements in Cargo.toml, including incompatible versions) and cargo set-version |
| policy: licenses, bans, duplicates, sources | cargo-deny | deny.toml; cargo deny check licenses bans sources advisories |
| known vulnerabilities | cargo-audit (RustSec) or cargo deny check advisories |
both need the RustSec advisory database (a Git repository fetched over the network) |
| faster, nicer test runs | cargo-nextest | each test in its own process, parallel across binaries, retries, JUnit output for CI |
| compile cache | sccache | RUSTC_WRAPPER=sccache; local disk, S3, GCS, Azure, Redis… |
| SemVer violations | cargo-semver-checks | run before publishing (Publishing) |
| feature combinations | cargo-hack | cargo hack check --each-feature, --feature-powerset catches features that don't build alone |
| unused dependencies | cargo-machete (fast, heuristic), cargo-udeps (precise, nightly) | |
| outdated dependencies | cargo-outdated, or Dependabot / Renovate in the repository | |
| MSRV | cargo-msrv | finds and verifies the minimum Rust version |
| binary size | cargo-bloat | which crates and functions take the space |
| macro expansion | cargo-expand | shows code after derive and macros |
| coverage | cargo-llvm-cov | LLVM source-based coverage, lcov/HTML output |
| cross builds | cross, cargo-zigbuild | see Cross-Compiling |
| releases | cargo-release, release-plz, cargo-dist | version bumps, changelogs, tags, publish, binary installers |
None of these third-party tools are installed on the course machine (and cargo-deny's and cargo-audit's advisory checks would need the network), so they're shown as commands, not sessions.
Two configurations worth copying
A starting deny.toml for cargo-deny:
[licenses]
allow = ["MIT", "Apache-2.0", "BSD-3-Clause", "ISC", "Unicode-3.0"]
[bans]
multiple-versions = "warn" # cargo tree -d as a CI signal
wildcards = "deny" # no `*` requirements
deny = [{ name = "openssl", reason = "use rustls" }]
[sources]
unknown-registry = "deny" # only crates.io (and listed private registries)
unknown-git = "deny"A CI test step with nextest (doc tests still need cargo test, nextest doesn't run them):
cargo nextest run --workspace --locked --profile ci # profiles live in .config/nextest.toml
cargo test --workspace --locked --docTest yourself
How does Cargo find the implementation of cargo deny?
- A plugin registry in Cargo.toml
- It runs an executable named
cargo-denyfrom PATH (or ~/.cargo/bin) - It downloads it from crates.io
- It's built into Cargo
What does RUSTC_WRAPPER=sccache change?
- Cargo uses sccache instead of the linker
- Cargo runs
sccache rustc <args>for every compiler call, letting sccache serve cached results - It enables incremental compilation
- It replaces Cargo
Which tool checks that each feature of a crate builds on its own?
- cargo-deny
- cargo-hack (--each-feature / --feature-powerset)
- cargo-audit
- cargo-binstall