Rust Build Profiles: Tune for Speed vs. Debugging
Rust build profiles are presets for the compiler, trading off compile time and debuggability for runtime speed. Use the `dev` profile for quick iteration and the `release` profile for production. The footgun is benchmarking without the `--release` flag.
WHY IT EXISTS Compiling code involves a fundamental trade-off. For development, you want fast compiles and rich debugging information. For production, you want the fastest possible runtime performance and a small binary. Build profiles exist to manage this tension by letting you define and switch between different sets of compiler settings for different situations.
THE MENTAL MODEL Think of build profiles as named presets for the Rust compiler, like 'development mode' and 'production mode'. Instead of manually passing flags like -C opt-level=3 for every build, you just tell Cargo which profile to use, and it applies the corresponding set of pre-configured options. The two you'll use 99% of the time are dev (the default) and release.
HOW IT WORKS Cargo has four built-in profiles: dev, release, test, and bench. When you run a command like cargo build, it automatically uses the dev profile. Running cargo build --release switches to the release profile. You can customize these profiles in your project's Cargo.toml file under the [profile] table. For example, [profile.dev] customizes the development profile. Key settings include opt-level (0-3, 's', 'z') for optimization level, debug for debug symbols, and overflow-checks to enable or disable integer overflow panics.
WHEN TO USE IT Use the dev profile for all day-to-day development. Its settings are optimized for fast compile times and a smooth debugging experience, with full debug symbols and checks enabled. Use the release profile whenever you need performance: for benchmarking, for shipping an application to users, or for deploying a service to production. The release profile enables aggressive optimizations that result in much faster code, at the cost of longer compile times.
WHEN NOT TO USE IT Never use the dev profile for performance testing or benchmarking. Its lack of optimization (opt-level = 0) will give you misleadingly slow results. Conversely, avoid using the release profile for your inner development loop. The long compile times will slow you down, and the aggressive optimizations can sometimes rearrange code in ways that make it harder to debug step-by-step.
ONE CANONICAL EXAMPLE A common mistake for newcomers is writing a computationally intensive function, running it with cargo run, and being disappointed by the performance. This is because cargo run uses the dev profile, which is unoptimized. Running the same code with cargo run --release can often result in a 10x to 100x speedup, as it switches to the release profile which defaults to opt-level = 3 (full optimizations).
Read the original → doc.rust-lang.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.