DEV Community

Cover image for [Rust Guide] 14.1. Cargo Publishing Configuration
SomeB1oody
SomeB1oody

Posted on

[Rust Guide] 14.1. Cargo Publishing Configuration

If you find this helpful, please like, bookmark, and follow. To keep learning along, follow this series.

14.1.1 Release Profile

A release profile is a publishing configuration, meaning it is a set of pre-defined configuration options. It is also customizable. We can define our own configurations and use different settings, giving programmers more control over how code is compiled.

Each profile is its own configuration preset and is independent from the others.

Cargo mainly has two profiles:

  • dev profile: used for development and cargo build
  • release profile: used for publishing and cargo build --release

Using cargo build and cargo build --release will apply two different configuration profiles:

$ cargo build
    Finished dev [unoptimized + debuginfo] target(s) in 0.0s
$ cargo build --release
    Finished release [optimized] target(s) in 0.0s
Enter fullscreen mode Exit fullscreen mode

14.1.2 Custom Profiles

Cargo provides default settings for each profile.

If you want to customize the configuration, whether for dev profile or release profile, you can add a [profile.xxxx] section to Cargo.toml and override a subset of the default settings. Usually we do not override every option; we only override the ones we want to change.

Here is an example:

[profile.dev]
opt-level = 0

[profile.release]
opt-level = 3
Enter fullscreen mode Exit fullscreen mode

The opt-level setting controls how much optimization Rust applies to your code, with values ranging from 0 to 3. Applying more optimizations increases compile time, so if you are developing and compiling code frequently, you may want fewer optimizations even if the generated code runs more slowly, because that speeds up compilation. That is why the default opt-level for dev is 0.

When you are ready to publish code, it is better to spend more time compiling. Code is compiled only once in release mode, but the compiled program runs many times, so release mode trades longer compile time for faster code. That is why the default opt-level for release is 3.

Top comments (0)