Conditional compilation
Conditional compilation selects source-code regions for inclusion or exclusion based on conditions evaluated during compilation or preprocessing.
Refactoring to Rust
The cfg attribute macro conditionally includes or excludes an annotated Rust item based on compile-time configuration.
- How it is used
- Uses compile-time conditions to select platform code, optional features, and test-only modules.
- How deep it goes
- Explains attribute placement and test-build behavior, but does not detail custom-flag configuration syntax.
- Examples
- Demonstrates #[cfg(test)] around a test module and features enabling Serde derive code.
- What you can do after
- Apply via Cargo and cfg attributes to exclude tests, select platform implementations, or enable optional dependency features.
Read how Refactoring to Rust explains conditional compilation →
Learn Rust in a Month of Lunches
A conditional compilation attribute instructs the Rust compiler to include or exclude an item according to a configuration condition.
- How it is used
- Uses configuration conditions to keep test code or platform-specific code out of builds where it does not apply.
- How deep it goes
- Explains attribute syntax and test-module organization with examples, but does not develop a broader condition grammar.
- Examples
- Shows #[cfg(test)], #[cfg(target_os = "windows")], and a conditional tests module containing assert-based tests.
- What you can do after
- Apply it via Rust’s cfg attribute to organize test-only and operating-system-specific code.
Read how Learn Rust in a Month of Lunches explains conditional compilation →
Rust in Action: Systems programming concepts and techniques
A cfg attribute is a Rust attribute that conditionally includes an item based on compile-time configuration predicates.
- How it is used
- Selects platform-specific code and data while building cross-platform command-line tools.
- How deep it goes
- Defines conditional compilation and works through cfg predicates and paired constants; does not detail compiler internals.
- Examples
- Uses Windows and non-Windows usage strings, debug output in development builds, and an OS-specific time-setting tool.
- What you can do after
- Practice/perform it by annotating alternative Rust items with cfg predicates for different compilation targets.
Code Like a Pro in Rust
A cfg attribute is a Rust compiler attribute that conditionally includes or excludes the attached source item according to configuration predicates.
- How it is used
- Selects code and attributes for optional Cargo features and OS-specific implementations.
- How deep it goes
- Defines cfg, cfg_attr, and cfg! and demonstrates their placement in code, without detailing compiler internals.
- Examples
- Shows feature-gated serde derives, Unix and Windows allocator branches, and a cfg! check for AVX2.
- What you can do after
- Apply it via Rust attributes and cfg! to gate feature-dependent or platform-specific code.
Read how Code Like a Pro in Rust explains conditional compilation →
Write Powerful Rust Macros
Conditional compilation is the selection of source items for compilation based on compile-time configuration conditions.
- How it is used
- Uses Cargo features to expose optional macros and exclude unneeded library or generated code.
- How deep it goes
- Explains feature declarations, cfg placement, dependency activation, and default features, without detailing compiler configuration internals.
- Examples
- Uses a feature-gated config_struct macro, struct_output module, ConfigStruct consumer, and Cargo feature declarations.
- What you can do after
- Configure optional Rust library capabilities and verify consumer code by enabling the required Cargo feature.
Read how Write Powerful Rust Macros explains conditional compilation →
Related concepts
- attribute macrokind of · An attribute macro is a compile-time procedural transformation attached to a language item that generates replacement or additional code.
- cargo featureconfigures · A Cargo feature is a named compile-time configuration option that conditionally enables code, dependencies, or capabilities in a Rust package.
- attributekind of · An attribute is a named property, value, or piece of information associated with an entity, object, or program element.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.




