Macro std::cfg_match

source ·
pub macro cfg_match {
    (
        $(cfg($initial_meta:meta) => { $($initial_tokens:item)* })+ _ => {
        $($extra_tokens:item)* }
    ) => { ... },
    ($(cfg($extra_meta:meta) => { $($extra_tokens:item)* })*) => { ... },
    (@ __items($($_:meta,)*);) => { ... },
    (
        @ __items($($no:meta,)*); (($($yes:meta)?) ($($tokens:item)*)),
        $($rest:tt,)*
    ) => { ... },
    (@ __identity $($tokens:item)*) => { ... },
}
🔬This is a nightly-only experimental API. (cfg_match #115585)
Expand description

A macro for defining #[cfg] match-like statements.

It is similar to the if/elif C preprocessor macro by allowing definition of a cascade of #[cfg] cases, emitting the implementation which matches first.

This allows you to conveniently provide a long list #[cfg]’d blocks of code without having to rewrite each clause multiple times.

Trailing _ wildcard match arms are optional and they indicate a fallback branch when all previous declarations do not evaluate to true.

§Example

#![feature(cfg_match)]

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