RustBrock/Custome Build Profiles.md
darkicewolf50 f3ec0d98cc
Some checks failed
Test Gitea Actions / first (push) Successful in 12s
Test Gitea Actions / check-code (push) Failing after 21s
Test Gitea Actions / test (push) Has been skipped
Test Gitea Actions / documentation-check (push) Has been skipped
finsihed ch14.1 and more than halfway through ch14.2
2025-02-25 17:02:50 -07:00

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 run cargo build
  • the release profile, Cargo uses this when you run cargo 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.