|
3 | 3 | Most Rust code depends on compiler optimization for good performance. This is
|
4 | 4 | true of many languages but it can be especially severe in Rust, which is
|
5 | 5 | specifically designed to use safe high-level abstractions that can be
|
6 |
| -optimized into efficient low-level code. Optimized Rust code very often runs |
7 |
| -10 to 50 times faster than the same code compiled without optimization. |
| 6 | +optimized into efficient low-level code. **Optimized Rust code often runs 10 |
| 7 | +to 50 times faster than the same code compiled without optimization.** |
8 | 8 |
|
9 |
| -The compiler's optimization passes can take a long time to run. Building a |
10 |
| -Rust project with optimization enabled often takes more than twice as long as |
11 |
| -an unoptimized build. So the Rust toolchain makes it easy to do unoptimized |
12 |
| -builds during development (when fast build time is important) and optimized |
13 |
| -builds only when for measuring performance or deploying to production. |
| 9 | +Building a Rust project with optimization enabled often takes more than twice |
| 10 | +as long as an unoptimized build. Optimized code can also be harder to debug |
| 11 | +in a debugger. So the Rust toolchain makes it easy to use unoptimized builds |
| 12 | +during development (when rapid compilation and easy debugging are important) |
| 13 | +and optimized builds when measuring performance or deploying to production. |
| 14 | + |
| 15 | +## Building optimized code |
| 16 | + |
| 17 | +To build a Cargo project with optimization enabled, use the `--release` flag: |
| 18 | + |
| 19 | +```sh |
| 20 | +cargo build --release |
| 21 | +``` |
| 22 | + |
| 23 | +Cargo will place the compiled output in the `target/release` directory, rather |
| 24 | +than the `target/debug` directory used for non-release builds. |
| 25 | + |
| 26 | +You can use the `--release` flag again to run an optimized binary: |
| 27 | + |
| 28 | +```sh |
| 29 | +cargo run --release |
| 30 | +``` |
| 31 | + |
| 32 | +This automatically compiles the program with optimization enabled (if it isn’t |
| 33 | +already compiled) and then runs it. |
14 | 34 |
|
15 | 35 | ## Cargo profiles
|
16 | 36 |
|
17 |
| -Cargo uses different compiler options for different commands. You can |
18 |
| -customize thes options the [profile] sections of the `Cargo.toml` file. There |
19 |
| -are five profiles. |
| 37 | +Cargo uses different compiler options for different commands. These options |
| 38 | +are grouped into five *profiles*. |
20 | 39 |
|
21 |
| -These profiles have optimization disabled by default: |
| 40 | +These commands compile **without** optimization by default: |
22 | 41 |
|
23 | 42 | ```sh
|
24 |
| -cargo build # uses the `dev` profile |
25 |
| -cargo test # uses the `test` profile |
26 |
| -cargo doc # uses the `doc` profile |
| 43 | +cargo build # uses `[profile.dev]` |
| 44 | +cargo test # uses `[profile.test]` |
| 45 | +cargo doc # uses `[profile.doc]` |
27 | 46 | ```
|
28 | 47 |
|
29 |
| -These profiles have optimization enabled by default: |
| 48 | +These commands compile **with** optimization by default: |
30 | 49 |
|
31 | 50 | ```sh
|
32 |
| -cargo build --release # uses the `release` profile |
33 |
| -cargo bench # uses the `bench` profile |
| 51 | +cargo build --release # uses `[profile.release]` |
| 52 | +cargo bench # uses `[profile.bench]` |
34 | 53 | ```
|
35 | 54 |
|
| 55 | +You can customize the compiler options for each command in the [profile] |
| 56 | +sections of the `Cargo.toml` file. |
| 57 | + |
36 | 58 | ### opt-level
|
37 | 59 |
|
| 60 | +TODO |
| 61 | + |
| 62 | +### opt-level |
| 63 | + |
| 64 | +TODO |
| 65 | + |
| 66 | +### codegen-units |
| 67 | + |
| 68 | +TODO |
| 69 | + |
| 70 | +### debug |
| 71 | + |
| 72 | +TODO |
| 73 | + |
| 74 | +### debug-assertions |
| 75 | + |
| 76 | +TODO |
| 77 | + |
| 78 | +### panic |
| 79 | + |
| 80 | +TODO |
38 | 81 |
|
39 | 82 |
|
40 | 83 | [profile]: http://doc.crates.io/manifest.html#the-profile-sections
|
0 commit comments