Search code examples
rustrust-cargo

What is the default optimization level of cargo build --release?


The rustc compiler has four optimization levels, just like GCC:

opt-level
This flag controls the optimization level.

0: no optimization, also turns on cfg(debug_assertions) (the default).
1: basic optimizations.
2: some optimizations.
3: all optimizations.
s: optimize for binary size.
z: optimize for binary size, but also turn off loop vectorization.

If I make a build with Cargo and its --release option, which optimization level is used?

cargo build --release
Finished release [optimized] target(s) in 0.75s

Solution

  • According to the cargo manual, the default level for release builds is -O3.

    # The release profile, used for `cargo build --release` (and the dependencies
    # for `cargo test --release`,  including the local library or binary).
    
    [profile.release]
    opt-level = 3
    debug = false
    split-debuginfo = '...'  # Platform-specific.
    debug-assertions = false
    overflow-checks = false
    lto = false
    panic = 'unwind'
    incremental = false
    codegen-units = 16
    rpath = false