Skip to content
tezvyn:
Menu

Rust Cargo Features: Conditional Compilation & Dependencies

Source: doc.rust-lang.orgHardHow cards are made

Cargo features are compile-time switches for conditional compilation and optional dependencies. They let you build tailored versions of a crate from one source, like an image library that only includes code for the formats you need.

Why it exists

Cargo features solve the problem of monolithic libraries. Without them, a crate would have to include all its code and dependencies for every possible use case, leading to slower compile times and larger binaries. Features allow a single crate to be flexible, serving many needs without forcing every user to pay the cost for functionality they don't use.

The mental model

Think of Cargo features as compile-time switches or optional toppings on a pizza. You declare in your Cargo.toml which features you want to enable for a dependency. The compiler then includes only the code and dependencies associated with those switches, giving you a customized build of the crate tailored to your exact needs.

How it works

Features are defined in a package's Cargo.toml file under the [features] table. Each feature is a name that maps to an array of other features or optional dependencies it enables. In your Rust code, you use the #[cfg(feature = "your_feature")] attribute to mark code that should only be compiled when that feature is active. Dependencies can be marked as optional with optional = true, which implicitly creates a feature of the same name. To avoid this, or to group dependencies under a different name, you can use the dep:dependency_name syntax inside a feature definition.

When to use it

Use features to provide optional functionality, such as serialization support via a serde feature. They are ideal for crates that support multiple formats (like an image library with png, jpeg, and webp features) or need to gate platform-specific code. They also allow you to offer different performance trade-offs by enabling or disabling certain optimizations.

When not to use it

Features are for compile-time configuration, not runtime choices. If a user needs to decide between options while the program is running, use standard language constructs like enums or trait objects. Avoid using features for mutually exclusive options, as this can lead to confusing build failures if a user accidentally enables both.

One canonical example

An image processing library, image-lib, wants to support PNG and WebP formats, but not force all users to compile both. It defines two features in its Cargo.toml: png = ["dep:png_crate"] and webp = ["dep:webp_crate"]. A user who only needs PNG support would declare the dependency as: image-lib = { version = "1.0", default-features = false, features = ["png"] }. This command ensures only the PNG-related code and its dependencies are compiled, resulting in a smaller, faster build.

Interview question

For which scenario would using Rust Cargo features be an inappropriate choice?

  • a.Allowing a user to select between two mutually exclusive data processing algorithms at runtime.Correct
  • b.Providing optional support for different image formats in a library.
  • c.Gating platform-specific code to compile only on certain operating systems.
  • d.Including optional dependencies like a serialization framework only when needed.
Why?

Cargo features are for compile-time configuration, not runtime choices. If a user needs to decide between options while the program is running, standard language constructs like enums or trait objects should be used instead. The other options describe valid use cases for Cargo features, as they involve compile-time conditional inclusion of code or dependencies.

Just read this? Test yourself on what you have been reading.

Read the original → doc.rust-lang.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on rust — each one lists the topics its interview covers.

See open roles