liveBook · Manning

Conditional compilation

Also called cfg

Conditional compilation selects source-code regions for inclusion or exclusion based on conditions evaluated during compilation or preprocessing.

How 5 Manning books explain it

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 target-specific code and controls debug output in portable Rust programs.
How deep it goes
Works through target-based definitions, predicate combinations, and debug output without examining compiler internals.
Examples
Uses operating-system-specific command usage strings and debug output from a CSV-processing program.
What you can do after
Apply it via Rust's cfg attributes to select platform-specific definitions and use cfg! to control debug output.

Read how Rust in Action: Systems programming concepts and techniques explains conditional compilation →

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 optional crate behavior and platform-specific code at compile time.
How deep it goes
Explains attribute scope, conditional attributes, boolean configuration tests, and predicate composition through implementation snippets.
Examples
Uses serde-gated imports and derives, Unix and Windows allocator paths, and an avx2 target-feature test.
What you can do after
Apply conditional compilation with Rust's cfg and cfg_attr attributes and cfg! macro to feature- and 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 →

Also covered in

What to know first

Related concepts

Questions about conditional compilation

What is conditional compilation?

Conditional compilation selects source-code regions for inclusion or exclusion based on conditions evaluated during compilation or preprocessing.

Which Manning books explain conditional compilation?

Refactoring to Rust, Learn Rust in a Month of Lunches, Rust in Action: Systems programming concepts and techniques, Code Like a Pro in Rust and Write Powerful Rust Macros.

What should I know before learning conditional compilation?

Manning books that teach conditional compilation usually introduce directive, complex arithmetic, crate and domain error first.

What is conditional compilation a kind of?

Conditional compilation is a kind of attribute macro. An attribute macro is a compile-time procedural transformation attached to a language item that generates replacement or additional code.

What is conditional compilation also called?

Conditional compilation is also called cfg.

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.