attribute

#[cfg] — Compile only selected code

Attach a configuration condition to an item. If the condition is false, that item is removed from the configured source. Unlike cfg!, this can exclude references to unavailable APIs. all, any and not combine predicates; Cargo features and target configuration are separate concepts. Result: Conditional item inclusion. Compiler flags: --cfg bytemind_preview. Expected output (not executed): 'preview\n' Common mistake: Using cfg! to guard code that cannot type-check on the current target.

Formal Syntax & Structure

rust syntaxrust
#[cfg(predicate)]

Working Example

Example Snippetrust
#[cfg(bytemind_preview)]fn label() -> &'static str { "preview" }#[cfg(not(bytemind_preview))]fn label() -> &'static str { "stable" }fn main() { println!("{}", label()); }
Rust Reference