65 lines
3.7 KiB
HTML
65 lines
3.7 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<title>Customizing Builds with Release Profiles</title>
|
||
</head>
|
||
<body>
|
||
<h2 id="customizing-builds-with-release-profiles"><a class="header" href="#customizing-builds-with-release-profiles">Customizing Builds with Release Profiles</a></h2>
|
||
<p>In Rust, <em>release profiles</em> are predefined, customizable profiles with
|
||
different configurations that allow a programmer to have more control over
|
||
various options for compiling code. Each profile is configured independently of
|
||
the others.</p>
|
||
<p>Cargo has two main profiles: the <code>dev</code> profile Cargo uses when you run <code>cargo build</code>, and the <code>release</code> profile Cargo uses when you run <code>cargo build --release</code>. The <code>dev</code> profile is defined with good defaults for development,
|
||
and the <code>release</code> profile has good defaults for release builds.</p>
|
||
<p>These profile names might be familiar from the output of your builds:</p>
|
||
<!-- manual-regeneration
|
||
anywhere, run:
|
||
cargo build
|
||
cargo build --release
|
||
and ensure output below is accurate
|
||
-->
|
||
<pre><code class="language-console">$ cargo build
|
||
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.00s
|
||
$ cargo build --release
|
||
Finished `release` profile [optimized] target(s) in 0.32s
|
||
</code></pre>
|
||
<p>The <code>dev</code> and <code>release</code> are these different profiles used by the compiler.</p>
|
||
<p>Cargo has default settings for each of the profiles that apply when you haven’t
|
||
explicitly added any <code>[profile.*]</code> sections in the project’s <em>Cargo.toml</em> file.
|
||
By adding <code>[profile.*]</code> sections for any profile you want to customize, you
|
||
override any subset of the default settings. For example, here are the default
|
||
values for the <code>opt-level</code> setting for the <code>dev</code> and <code>release</code> profiles:</p>
|
||
<p><span class="filename">Filename: Cargo.toml</span></p>
|
||
<pre><code class="language-toml">[profile.dev]
|
||
opt-level = 0
|
||
|
||
[profile.release]
|
||
opt-level = 3
|
||
</code></pre>
|
||
<p>The <code>opt-level</code> setting controls the number of optimizations Rust will apply to
|
||
your code, with a range of 0 to 3. Applying more optimizations extends
|
||
compiling time, so if you’re in development and compiling your code often,
|
||
you’ll want fewer optimizations to compile faster even if the resultant code
|
||
runs slower. The default <code>opt-level</code> for <code>dev</code> is therefore <code>0</code>. When you’re
|
||
ready to release your code, it’s best to spend more time compiling. You’ll only
|
||
compile in release mode once, but you’ll run the compiled program many times,
|
||
so release mode trades longer compile time for code that runs faster. That is
|
||
why the default <code>opt-level</code> for the <code>release</code> profile is <code>3</code>.</p>
|
||
<p>You can override a default setting by adding a different value for it in
|
||
<em>Cargo.toml</em>. For example, if we want to use optimization level 1 in the
|
||
development profile, we can add these two lines to our project’s <em>Cargo.toml</em>
|
||
file:</p>
|
||
<p><span class="filename">Filename: Cargo.toml</span></p>
|
||
<pre><code class="language-toml">[profile.dev]
|
||
opt-level = 1
|
||
</code></pre>
|
||
<p>This code overrides the default setting of <code>0</code>. Now when we run <code>cargo build</code>,
|
||
Cargo will use the defaults for the <code>dev</code> profile plus our customization to
|
||
<code>opt-level</code>. Because we set <code>opt-level</code> to <code>1</code>, Cargo will apply more
|
||
optimizations than the default, but not as many as in a release build.</p>
|
||
<p>For the full list of configuration options and defaults for each profile, see
|
||
<a href="https://doc.rust-lang.org/cargo/reference/profiles.html">Cargo’s documentation</a>.</p>
|
||
</body>
|
||
</html>
|