TExcelTAD
Home › Cargo in Depth › Profiles: dev, release & Custom

Profiles: dev, release & Custom

A profile is a named set of compiler settings: optimisation level, debug info, overflow checks, LTO, panic strategy. Cargo has four built in and lets you define your own. Profiles are where "it's slow in debug", "the binary is huge" and "release behaves differently from my tests" get decided.

The built-in profiles

Profile Used by Directory Key defaults
dev cargo build, cargo run, cargo check target/debug/ opt-level = 0, debug = true, debug-assertions = true, overflow-checks = true, incremental
release --release target/release/ opt-level = 3, debug = false, no overflow checks or debug assertions
test cargo test target/debug/ inherits dev
bench cargo bench target/release/ inherits release

The settings that matter

Setting Values Effect
opt-level 0–3, "s", "z" optimisation; s/z optimise for size
debug false, "line-tables-only", "limited", true debug info for debuggers and backtraces
strip "none", "debuginfo", "symbols" (or true) remove debug info / symbols from the binary
lto false, "thin", true/"fat", "off" link-time optimisation across crates: smaller, faster, slower to link
codegen-units integer (release default 16) fewer units = better optimisation, slower compile; 1 for maximum
panic "unwind", "abort" unwinding (catchable, runs destructors) or immediate abort (smaller)
overflow-checks bool panic on integer overflow instead of wrapping
debug-assertions bool enables debug_assert! and cfg(debug_assertions)
incremental bool the compiler's incremental cache

Behaviour can differ between profiles

Integer overflow is the classic example: the same code panics in a debug build and silently wraps in release.

bash · try it yourself
cargo new sum --vcs none -q
cd sum
cat > src/main.rs <<'RS'
fn main() {
    let v: Vec<u8> = std::env::args().skip(1).map(|a| a.parse().unwrap()).collect();
    let total: u8 = v.iter().sum();
    println!("total = {total}");
}
RS
echo "--- dev"
cargo run -q -- 200 100 2>&1 | grep -o 'attempt to add with overflow'
echo "--- release"
cargo run -q --release -- 200 100
--- dev
attempt to add with overflow
--- release
total = 44

200 + 100 doesn't fit in a u8: debug panics, release prints 44 (300 mod 256). Tests run with the test profile (like dev), so they would catch this, but production would not. For code where that matters, use checked_add/wrapping_add explicitly, or turn overflow-checks on in release.

Custom profiles and a size-tuned release

A custom profile needs inherits. Here release is tuned for a small, fast binary, and a ci profile is release plus all the runtime checks:

bash · try it yourself
cargo new sum --vcs none -q
cd sum
cat > src/main.rs <<'RS'
fn main() {
    let v: Vec<u8> = std::env::args().skip(1).map(|a| a.parse().unwrap()).collect();
    let total: u8 = v.iter().sum();
    println!("total = {total}");
}
RS
cat >> Cargo.toml <<'T'

[profile.release]
lto = "fat"
codegen-units = 1
panic = "abort"
strip = true

[profile.ci]
inherits = "release"
overflow-checks = true
debug-assertions = true
T
cargo build --profile ci 2>&1
ls target
bash -c './target/ci/sum 200 100 2>err.txt; echo "exit code: $?"' 2>/dev/null
grep -o 'attempt to add with overflow' err.txt
echo "--- sizes"
cargo build -q
cargo build -q --release
stat -c '%s bytes  %n' target/debug/sum target/release/sum
   Compiling sum v0.1.0 (/home/learner/sum)
    Finished `ci` profile [optimized] target(s) in 2.39s
CACHEDIR.TAG
ci
exit code: 134
attempt to add with overflow
--- sizes
4654352 bytes  target/debug/sum
293512 bytes  target/release/sum
  • The ci profile got its own directory, target/ci/, and Finished names it.
  • With panic = "abort" the overflow panic killed the process with SIGABRT: exit code 134 (128 + 6) instead of the usual 101 for an unwinding panic. Abort means no destructors run and catch_unwind doesn't work; in return the binary loses its unwinding tables.
  • The release binary is about 16 times smaller than the debug one. Most of the difference is debug info and symbols (strip), the rest is optimisation and LTO.

Per-package overrides

Profiles can be adjusted for specific packages. The most popular use: keep your own code unoptimised (fast incremental builds, good debugging) but optimise dependencies in debug builds, because an unoptimised image decoder or regex engine makes the debug binary painfully slow:

toml
[profile.dev.package."*"]        # every dependency, not workspace members
opt-level = 2

[profile.dev.package.image]      # or one specific crate
opt-level = 3

[profile.dev.build-override]     # build scripts and proc-macros
opt-level = 3

Dependencies rarely change, so their optimised builds are cached and the cost is paid once.

Overriding from the environment

Every profile setting can be set without editing the manifest, which is useful in CI: CARGO_PROFILE_RELEASE_LTO=true cargo build --release, or --config 'profile.release.debug="line-tables-only"' on the command line. Remember that profiles are only read from the workspace root manifest.

Tip: debug = "line-tables-only" in release gives readable panic backtraces and profiler output for a fraction of the size of full debug info.

Test yourself

let x: u8 = 200; let y = x + 100; with default profiles: what happens in cargo run vs cargo run --release?

  1. Both panic
  2. Debug panics, release wraps to 44
  3. Both wrap
  4. Release panics, debug wraps

What does a custom [profile.ci] need so Cargo accepts it?

  1. A dir key
  2. inherits = "release" (or another profile)
  3. Nothing
  4. To be listed in [workspace]

Debug builds are too slow because a dependency does heavy work unoptimised. The least invasive fix?

  1. Always build with --release
  2. [profile.dev.package."*"] opt-level = 2
  3. Set opt-level = 3 for dev globally
  4. Vendor the dependency