Sub topic 4
Tuning the dev profile
This chapter assumes you can build and test a Cargo project and edit Cargo.toml. You should also know that cargo build uses the dev profile by default.
What the dev profile controls
A Cargo profile is a named set of compiler settings. The dev profile is used by cargo build, cargo run and cargo test. By default it uses opt-level = 0 (no optimization) and debug = true (full debug info). It also turns on incremental compilation.
Debug info is the cost to look at first. It records variable names, types and source locations so a debugger can show them. Generating it takes work in rustc and LLVM, and the larger object files take longer for the linker to process.
Reduce debug info
Most projects do not need full variable inspection on every build. The line-tables-only setting keeps file and line information, so panics and backtraces still point to the right source lines. It drops the type and variable data that a debugger uses.
# Cargo.toml
[profile.dev]
# Keep line numbers for backtraces but skip full variable debug info.
debug = "line-tables-only"If you rarely use a debugger, you can set debug = false for the smallest output. Backtraces then lose line numbers. Measure the effect with cargo build --timings before and after. The gain depends on how much time goes to codegen and linking in your project.
Optimize dependencies only
Unoptimized code can run very slowly, especially parsing, hashing, compression and iterator-heavy code. A per-package override lets you optimize third-party crates while your own crate stays at opt-level = 0.
# Cargo.toml
[profile.dev.package."*"]
# Optional: optimize third-party dependencies while your own code stays fast to build.
opt-level = 2The "*" pattern matches every dependency, but it does not match workspace members. Dependencies are compiled once and then cached, so you pay the extra compile time on the first build and after a dependency changes. Your own code keeps its fast rebuilds. This trade is worth it when tests or the running app feel slow in debug mode.
Common mistakes
- Optimizing your own crate in
devto fix slow tests. This slows every rebuild. Optimize only the dependencies that are hot. - Setting
debug = falseand then being unable to read backtraces. Useline-tables-onlyfirst. - Changing profile settings without measuring. Compare
cargo build --timingsoutput from a clean build each time. - Forgetting that profile settings are read only from the workspace root
Cargo.toml. Profiles in member crates are ignored.
Choosing settings
| Goal | Setting |
|---|---|
| Fastest build, backtraces with lines | `debug = "line-tables-only"` |
| Fastest build, no debugger or line info needed | `debug = false` |
| Full debugger use | `debug = true` (default) |
| Faster runtime of dependencies in debug | `[profile.dev.package."*"] opt-level = 2` |
Exercise
Your debug-build tests spend most of their time inside a third-party JSON parser, but you edit your own crate constantly. Which profile change helps without slowing your rebuilds, and why?
Show answer
Add [profile.dev.package."*"] with opt-level = 2. The parser is optimized once and cached, while your own crate stays at opt-level = 0, so your incremental rebuilds remain fast.
Exercise
What do you lose by choosing debug = "line-tables-only" instead of the default?
Show answer
You lose type and variable debug info, so a debugger cannot inspect variables well. Backtraces and panic locations still show file and line numbers.