Files
.gitea
.github
.obsidian
HelloWorld
actix_web_learning
adder
branches
functions_rust
guessing_game
hello-async
hello_cargo
loops
minigrep
ownership
rectangles
.gitignore
Advanced Features.md
Advanced Functions and Closures.md
Advanced Traits.md
Advanced Types.md
Any Number of Futures.md
Applying Async.md
Async, Await, Futures and Streams.md
Cargo Workspaces.md
Cargo and Cratesio.md
Characteristics of OO Languages.md
Closures.md
Collection of Common Data Structs.md
Concurrency.md
Constants.md
Crates.md
Custome Build Profiles.md
Data Types.md
Deref Trait.md
Drop Trait.md
Enums.md
Error Handling.md
Extending Cargo.md
For later baja.md
Futures and Async.md
Futures in Sequence.md
Futures, Tasks and Threads Together.md
Generic Types Traits and Lifetimes.md
Generics.md
Hash.md
Implementing OO Design Pattern.md
Improving The IO Project.md
Install Binaries.md
Iterators and Closures.md
Iterators.md
Leaky Reference Cycles.md
Lifetimes.md
Macros.md
Modules and Use.md
OOP Programming Features.md
Packages.md
Passing Data Between Threads.md
Paths.md
Pattern Matching.md
Pattern Syntax.md
Places Patterns Can Be Used.md
Primitives.md
Project Organization.md
Publishing libraries.md
README.md
Reducing_Code_Duplication.md
Ref Cell Mutability.md
Reference Counter Smart Pointer.md
Refutability.md
RustBrock.code-workspace
Shared State Concurrency.md
Simultaneous Code Running.md
Smart Pointers.md
String.md
Structures.md
Sync and Send.md
Test Controls.md
Test_Organization.md
Tests.md
The Performance Closures and Iterators.md
Trait Objects that allow for Values of Different Types.md
Traits for Async.md
Traits.md
Unsafe Rust.md
Using Box on the Heap.md
Variables.md
Vector.md
Writing_Tests.md
data_types.md
ownership.md
RustBrock/Custome Build Profiles.md

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.