2.7 KiB
Customizing Builds with Release Profiles
Rust has release profiles which are predefined and customizable profiles with different configuations that allow a programmer to have more control over various options for compiling code.
Each profile is configured independently of others.
Cargo has two main profiles
- the
dev
profile, Cargo uses this when you runcargo build
- the
release
profile, Cargo uses this when you runcargo build --release
The dev
profile is defined with good defaults for development
The release
profile has good defaults for release builds
These profile names are present and fimilar from the output of builds
$ cargo build
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.00s
$ cargo build --release
Finished `release` profile [optimized] target(s) in 0.32s
Cargo has default seeting for each of the profiles that apply when you havent specified any [profile.*]
sections in the project's Cargo.toml file
By adding [profile.*]
sections you want to customize, you override any subset of the default settings
For example here are the default values for the opt-level
setting for the dev
and release
profiles
[profile.dev]
opt-level = 0
[profile.release]
opt-level = 3
The opt-level
setting controlsthe nubmer of optimizations the Rust compiler will apply to your code
This has a range of 0 - 3
Applying more optimizations extends the compiling time.
If you are compiling your code often and want fewer optimizations to compile even if the resulting executable runs slower.
This then makes the default opt-level
for dev
0
becasue you want to spend less time compiling.
When you are ready to realse your code it is best to spend more time compiling.
You will only optimize the compilation and spend mroe time because you will run that optimized program many times, so release mode trades longer compile time for code tha runs faster.
The default opt-level
for release
is therefore 3
because of that fact.
You can override a default seeting by adding a different value for it in Cargo.toml
For example if we wanted to use the optimization level 1 in the dev
profile
We can add these lines to the project's Cargo.toml file
[profile.dev]
opt-level = 1
This overrides the defualt setting of 0
Now when you run cargo build
cargo will use the defaults for the dev
profile and our customization to the opt-level
This is because of the specifiaction we set to the opt-level
.
In this customization Cargo will apply more optimization than the default, but still not as many compaired to the release build.
For the full list of config options nad defaults for each profile, see Cargo's documentation.