Features and Profiles
Conditional compilation.
Features and Profiles is a free Learn Rust Coding lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn Rust Coding learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Are Features?
Features let you enable or disable optional parts of a crate at compile time. They make crates flexible: callers pay only for what they use.
Declaring Features
Define features in the [features] table. Each feature can turn on other features or optional dependencies.
[features]
default = ["json"]
json = []
fast_math = []Conditional Compilation
Guard code with the cfg attribute so it compiles only when a feature is enabled.
#[cfg(feature = "json")]
fn export() {
println!("Exporting as JSON");
}
fn main() {
#[cfg(feature = "json")]
export();
}Enabling Features
Callers enable features in their dependency declaration or on the command line.
[dependencies]
my_lib = { version = "1.0", features = ["fast_math"] }Default Features
The default feature is on automatically. Disable it with default-features = false to opt into a minimal build.
[dependencies]
my_lib = { version = "1.0", default-features = false }Features on the CLI
You can also toggle features per command without editing the manifest.
cargo build --features fast_math
cargo run --no-default-features
cargo test --all-featuresWhat Are Profiles?
Profiles control compiler settings like optimization level and debug info. The two built-in profiles are dev (debug) and release.
The dev Profile
The dev profile is used by cargo build. It favors fast compilation and debuggability over runtime speed.
[profile.dev]
opt-level = 0
debug = trueThe release Profile
The release profile (used with --release) turns on optimizations for fast binaries.
[profile.release]
opt-level = 3
lto = true
strip = trueTuning a Profile
You can fine-tune any profile. For example, enable a bit of optimization in dev builds for faster runtime while keeping quick compiles for dependencies.
[profile.dev]
opt-level = 1
[profile.dev.package."*"]
opt-level = 3Optional Dependencies as Features
An optional dependency implicitly creates a feature of the same name. Enabling the feature pulls in the dependency, so users opt into heavier crates only when needed.
[dependencies]
serde = { version = "1.0", optional = true }
[features]
serialize = ["dep:serde"]Quick Check
Which attribute compiles a block of code only when a feature is enabled?
Recap
You learned conditional compilation tools:
[features]declares optional functionality#[cfg(feature = ...)]gates code by featuredefault-features = falseopts into minimal builds- Profiles like
devandreleasecontrol optimization and debug settings
Frequently asked questions
Is the “Features and Profiles” lesson free?
Yes — the full text of “Features and Profiles” is free to read here on the web, and the Learn Rust Coding course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn Rust Coding course, upgrade to CoddyKit PRO.
What will I learn in “Features and Profiles”?
Conditional compilation. You practise Learn Rust Coding with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn Rust Coding?
No prior experience is required. Learn Rust Coding on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Features and Profiles” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn Rust Coding lesson?
Yes. Every Learn Rust Coding lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Cargo Basics
- Cargo.toml
- Workspaces
- Features and Profiles