cfg_match in std - Rust (original) (raw)

pub macro cfg_match {
    ({ <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span></span></span></span>tt:tt)* }) => { ... },
    (_ => { <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span></span></span></span>output:tt)* }) => { ... },
    ($cfg:meta => <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>o</mi><mi>u</mi><mi>t</mi><mi>p</mi><mi>u</mi><mi>t</mi><mo>:</mo><mi>t</mi><mi>t</mi></mrow><annotation encoding="application/x-tex">output:tt </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8095em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">o</span><span class="mord mathnormal">u</span><span class="mord mathnormal">tp</span><span class="mord mathnormal">u</span><span class="mord mathnormal">t</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">:</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.6151em;"></span><span class="mord mathnormal">tt</span></span></span></span>($($rest:tt)+)?) => { ... },
}

🔬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! {
    unix => {
        fn foo() { /* unix specific functionality */ }
    }
    target_pointer_width = "32" => {
        fn foo() { /* non-unix, 32-bit functionality */ }
    }
    _ => {
        fn foo() { /* fallback implementation */ }
    }
}

If desired, it is possible to return expressions through the use of surrounding braces:

#![feature(cfg_match)]

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