Rust Reference ยท Tools
Cargo profiles and features
Reference for Cargo profiles (dev, release, custom) and features: opt-level, LTO, debug assertions, overflow checks, optional dependencies and cfg(feature).
A profile is a named set of compiler settings. Cargo ships dev (used by cargo build: fast compiles, debug info, overflow checks on) and release (used by cargo build --release: optimized, slower to compile). You tune them in the [profile.*] tables of Cargo.toml.
A feature is a named switch for optional code or optional dependencies. Enabling a feature passes --cfg 'feature="name"' to the compiler, so #[cfg(feature = "name")] includes code only when it is on. This page is a read-only reference; the examples show the compile-time effects.
[profile.release] opt-level = 3 lto = "thin" codegen-units = 1 panic = "abort" [features] default = ["json"] json = ["dep:serde_json"]
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Syntax
| Setting | Description |
|---|---|
opt-level |
0 (none), 1, 2, 3 (default for release), "s" and "z" (size). |
debug |
Debug info: true/2, 1, "line-tables-only", false. |
debug-assertions, overflow-checks |
On in dev, off in release by default. |
lto |
Link-time optimization: false, "thin", true (fat), "off". |
codegen-units |
Fewer units give better optimization and slower builds (1 for max). |
panic |
"unwind" (default) or "abort" (smaller binary, no catching). |
strip |
"none", "debuginfo", "symbols" to shrink binaries. |
inherits |
Required for custom profiles: [profile.fast] inherits = "release". |
[profile.dev.package."*"] |
Per-dependency overrides, for example optimize dependencies in dev builds. |
Feature syntax: default = [...] lists what is on unless disabled; a = ["b"] makes a imply b; x = ["dep:crate"] ties an optional dependency to x; y = ["crate/feat"] turns on a feature of a dependency.
Usage notes
- Profiles are only read from the workspace root manifest; members'
[profile]tables are ignored. cargo testuses thetestprofile (inheritsdev);cargo benchusesbench(inheritsrelease).- Features must be additive: enabling more of them must never remove functionality, because Cargo unifies features across the whole graph.
- Build with
--no-default-features --features a,bto select exactly;--all-featuresturns everything on (used in CI). - A
panic = "abort"profile makescatch_unwinduseless and skips unwinding destructors; tests need unwinding. cfg!(feature = "x")is an expression that evaluates to a bool, while#[cfg(feature = "x")]removes code entirely. Code undercfg!must still compile.- See cargo bench for measuring, and Cargo.toml for the other tables.