Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help


x-i18n: generated_at: “2026-03-01T14:25:40Z” model: gemini-3-flash-preview provider: google-gemini-cli source_hash: 89c703f79284a38445f136feede175a251d0161b1c0efb3d49aa84344af04bf0 source_path: ch14-01-release-profiles.md workflow: 16

使用发布配置自定义构建 (Customizing Builds with Release Profiles)

Customizing Builds with Release Profiles

在 Rust 中,“发布配置 (release profiles)”是预定义的、可定制的配置,具有不同的设置,允许程序员更好地控制编译代码的各种选项。每个配置都是独立设置的。

In Rust, release profiles 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.

Cargo 有两个主要的配置:一个是当你运行 cargo build 时 Cargo 使用的 dev 配置,另一个是当你运行 cargo build --release 时 Cargo 使用的 release 配置。dev 配置定义了适合开发的良好默认值,而 release 配置定义了适合发布构建的良好默认值。

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 development, and the release profile has good defaults for release builds.

这些配置名称你可能从构建输出中感到熟悉:

These profile names might be familiar from the output of your 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

devrelease 就是编译器使用的这些不同的配置。

The dev and release are these different profiles used by the compiler.

Cargo 为每个配置提供了默认设置,当你没有在项目的 Cargo.toml 文件中显式添加任何 [profile.*] 部分时,这些设置就会生效。通过为你想要自定义的任何配置添加 [profile.*] 部分,你可以覆盖默认设置的任何子集。例如,以下是 devrelease 配置中 opt-level 设置的默认值:

Cargo has default settings for each of the profiles that apply when you haven’t explicitly added any [profile.*] sections in the project’s Cargo.toml file. By adding [profile.*] 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 opt-level setting for the dev and release profiles:

文件名: Cargo.toml

[profile.dev]
opt-level = 0

[profile.release]
opt-level = 3

opt-level 设置控制 Rust 将应用于代码的优化程度,范围为 0 到 3。应用更多优化会延长编译时间,因此如果你处于开发阶段并经常编译代码,你会希望减少优化以加快编译速度,即使生成的代码运行速度较慢。因此,dev 的默认 opt-level0。当你准备发布代码时,最好花更多时间进行编译。你只需在发布模式下编译一次,但你会多次运行编译后的程序,因此发布模式是以更长的编译时间换取更快的运行速度。这就是为什么 release 配置的默认 opt-level3 的原因。

The opt-level 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 opt-level for dev is therefore 0. 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 opt-level for the release profile is 3.

你可以通过在 Cargo.toml 中为其添加不同的值来覆盖默认设置。例如,如果我们想在开发配置中使用优化级别 1,我们可以将这两行添加到项目的 Cargo.toml 文件中:

You can override a default setting by adding a different value for it in Cargo.toml. For example, if we want to use optimization level 1 in the development profile, we can add these two lines to our project’s Cargo.toml file:

文件名: Cargo.toml

[profile.dev]
opt-level = 1

这段代码覆盖了默认设置 0。现在当我们运行 cargo build 时,Cargo 将使用 dev 配置的默认值加上我们对 opt-level 的自定义。因为我们将 opt-level 设置为 1 ,Cargo 将应用比默认情况更多的优化,但不如发布构建中的那么多。

This code overrides the default setting of 0. Now when we run cargo build, Cargo will use the defaults for the dev profile plus our customization to opt-level. Because we set opt-level to 1, Cargo will apply more optimizations than the default, but not as many as in a release build.

有关每个配置的完整配置选项列表和默认值,请参阅 Cargo 文档

For the full list of configuration options and defaults for each profile, see Cargo’s documentation.