23.6 Build Profiles
Different profiles provide varying levels of optimization and debug information. By default, Cargo includes two main profiles:
- dev (default for
cargo build
): Faster compilation, fewer optimizations. - release (invoked with
cargo build --release
): High-level optimizations for production.
Customize these in Cargo.toml
:
[profile.dev]
opt-level = 0
debug = true
[profile.release]
opt-level = 3
debug = false
lto = true
opt-level
: Ranges from0
(no optimizations) to3
(maximum).debug = true
: Embeds debug symbols in the binary.lto = true
: Enables link-time optimization (can improve performance and reduce binary size).
Cargo also provides profiles for tests and benchmarks (discussed in the next chapter). You can create custom profiles as well, though they’re less commonly needed. Keep in mind that Cargo only considers profile settings from the project’s top-level Cargo.toml
; dependencies usually ignore their own profile settings so that the entire build is consistent.