Macro cfg_select

Source
pub macro cfg_select($($tt:tt)*) {
    ...
}
🔬This is a nightly-only experimental API. (cfg_select #115585)
Expand description

Selects code at compile-time based on cfg predicates.

This macro evaluates, at compile-time, a series of cfg predicates, selects the first that is true, and emits the code guarded by that predicate. The code guarded by other predicates is not emitted.

An optional trailing _ wildcard can be used to specify a fallback. If none of the predicates are true, a compile_error is emitted.

§Example

#![feature(cfg_select)]

cfg_select! {
    unix => {
        fn foo() { /* unix specific functionality */ }
    }
    target_pointer_width = "32" => {
        fn foo() { /* non-unix, 32-bit functionality */ }
    }
    _ => {
        fn foo() { /* fallback implementation */ }
    }
}

The cfg_select! macro can also be used in expression position:

#![feature(cfg_select)]

let _some_string = cfg_select! {
    unix => { "With great power comes great electricity bills" }
    _ => { "Behind every successful diet is an unwatched pizza" }
};