In Rust, release profiles are predefined and customizable profiles withdifferent configurations that allow a programmer to have more control overvarious options for compiling code. Each profile is configured independently ofthe others.
Cargo has two main profiles: the dev
profile Cargo uses when you run cargo build
and the release
profile Cargo uses when you run cargo build --release
. The dev
profile is defined with good defaults for developing, andthe release
profile has good defaults for release builds.
These profile names might be familiar from the output of your builds, whichshows the profile used in the build:
$ cargo build Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs$ cargo build --release Finished release [optimized] target(s) in 0.0 secs
The dev
and release
shown in this build output indicate that the compileris using different profiles.
Cargo has default settings for each of the profiles that apply when therearen’t any [profile.*]
sections in the project’s Cargo.toml file. By adding[profile.*]
sections for any profile we want to customize, we can overrideany subset of the default settings. For example, here are the default valuesfor the opt-level
setting for the dev
and release
profiles:
Filename: Cargo.toml
[profile.dev]opt-level = 0[profile.release]opt-level = 3
The opt-level
setting controls the number of optimizations Rust will apply toyour code with a range of zero to three. Applying more optimizations extendscompiling time, so if you’re in development and compiling your code often, youwant faster compiling even at the expense of the resulting code running slower.That is the reason the default opt-level
for dev
is 0
. When you’re readyto release your code, it’s best to spend more time compiling. You’ll onlycompile in release mode once and run the compiled program many times, sorelease mode trades longer compile time for code that runs faster. That is thereason the default opt-level
for the release
profile is 3
.
We can override any default setting by adding a different value for it inCargo.toml. For example, if we want to use optimization level 1 in thedevelopment profile, we can add these two lines to our project’s Cargo.tomlfile:
Filename: Cargo.toml
[profile.dev]opt-level = 1
This code overrides the default setting of 0
. Now when we run cargo
build
, Cargo will use the defaults for the dev
profile plus ourcustomization to opt-level
. Because we set opt-level
to 1
, Cargo willapply more optimizations than the default, but not as many as a release build.
For the full list of configuration options and defaults for each profile, seeCargo’s documentation.