23.6 Build Profiles

Different profiles offer varying levels of optimization and debug information. Cargo provides two primary profiles by default:

  • dev (default for cargo build): Faster compilation, minimal optimizations.
  • release (invoked with cargo build --release): Higher optimizations, better runtime performance.

Customize these in Cargo.toml:

[profile.dev]
opt-level = 0
debug = true

[profile.release]
opt-level = 3
debug = false
lto = true
  • opt-level: Ranges from 0 (no optimizations) to 3 (maximum).
  • debug: When true, embeds debug symbols in the binary.
  • lto: Link-time optimization, which can improve performance and reduce binary size.

Cargo also has profiles for tests and benchmarks (covered in the next chapter). Note that Cargo only applies profile settings from the top-level Cargo.toml of your project; dependencies typically ignore their own profile settings.