Skip to main content

cfg_attr

Attribute cfg_attr 

Source
Expand description

The cfg_attr attribute is used to conditionally apply one or more attributes to an item.

Example:

// The struct derives `Debug` when the `debug_impls` feature is enabled.
#[cfg_attr(feature = "debug_impls", derive(Debug))]
struct X;

You can apply multiple attributes by separating them with commas:

#[cfg_attr(feature = "nightly", allow(dead_code), deny(unused_variables))]
// This function only gets the `allow(dead_code)` and `deny(unused_variables)` attributes when
// `feature = "nightly"` is active.
fn nightly_only_function() {
    let x = 42;
}

For complex conditions, you can combine all(...), any(...), and not(...).

  • all: True if all given predicates are true.
  • any: True if at least one of the given predicates is true.
  • not: True if the predicate is false.
#[cfg_attr(
    all(feature = "system", feature = "disk"),
    doc = "These docs only show up if both `system`, and `disk` are enabled.",
)]
mod my_module {
    // ...
}

For more information, see the Reference on the cfg_attr attribute.