Cargo Configuration
While Cargo.toml declares the portable manifest of a package, .cargo/config.toml configures local environment, build flags, command aliases, custom target runners, and registry mirrors without altering the package manifest itself.
Cargo Hierarchical Configuration Resolution (Lowest to Highest Precedence):
1. ~/.cargo/config.toml (User-global defaults)
2. /repo/.cargo/config.toml (Workspace / repository config)
3. /repo/sub/.cargo/config.toml (Subdirectory config)
4. CARGO_* Environment Variables (e.g., `CARGO_BUILD_JOBS=4`)
5. `--config key=val` CLI Flags (Highest precedence)
Note that the file should always be named .cargo/config.toml (with the .toml extension; the legacy extensionless .cargo/config is deprecated).
Common .cargo/config.toml Sections
# File: .cargo/config.toml
# 1. Custom command aliases (`cargo t-all`, `cargo xtask`, `cargo lint`)
[alias]
t-all = "test --workspace --all-targets --all-features"
lint = "clippy --workspace --all-targets --all-features -- -D warnings"
xtask = "run --package xtask --"
# 2. Default build settings
[build]
# Set a default target triple or extra rustflags
# target = "x86_64-unknown-linux-gnu"
rustdocflags = ["-D", "warnings"]
# 3. Target-specific linkers, rustflags, and test runners
[target.x86_64-unknown-linux-gnu]
# Use fast `lld` or `mold` linker on Linux when available
linker = "clang"
rustflags = ["-C", "link-arg=-fuse-ld=lld"]
[target.wasm32-wasip1]
# Run `cargo test --target wasm32-wasip1` automatically inside `wasmtime`
runner = "wasmtime run --"
# 4. Environment variables injected into Cargo builds and tests
[env]
APP_TEST_DATA_DIR = { value = "tests/fixtures", relative = true }
# 5. Network retry and sparse registry protocol settings
[net]
retry = 3
git-fetch-with-cli = true
The cargo xtask Automation Pattern
Defining xtask = "run --package xtask --" under [alias] in .cargo/config.toml is a widely used Rust workspace pattern (used by rust-analyzer, tokio, and cargo itself). Instead of maintaining fragile Bash + PowerShell scripts for custom CI tasks, codegen, or packaging, you create an internal binary crate xtask/ in your workspace and invoke it portably across macOS, Linux, and Windows via cargo xtask <subcommand>:
// Example `xtask/src/main.rs` task dispatcher invoked via `cargo xtask dist`:
fn run_xtask(args: &[&str]) -> Result<&'static str, String> {
match args {
["dist"] => Ok("Building release binaries and generating checksums..."),
["coverage"] => Ok("Running test suite with LLVM source-based code coverage..."),
["codegen"] => Ok("Regenerating schema bindings..."),
other => Err(format!("Unknown xtask command: {other:?}")),
}
}
fn main() {
println!("{}", run_xtask(&["dist"]).unwrap());
}
Building release binaries and generating checksums...
Overriding Any Config Key on the Command Line (--config)
Any setting supported in .cargo/config.toml can be overridden for a single command invocation using --config <KEY>=<TOML_VALUE>:
# Override build.jobs and profile.release.lto for a single CI command
cargo build --release --config 'build.jobs=4' --config 'profile.release.lto="thin"'