Macro switch

Source
macro_rules! switch {
    ({ $($tt:tt)* }) => { ... };
    (_ => { $($output:tt)* }) => { ... };
    (
        $cond:path => $output:tt
        $($( $rest:tt )+)?
    ) => { ... };
    (
        #[cfg($cfg:meta)] => $output:tt
        $($( $rest:tt )+)?
    ) => { ... };
}
Expand description

Provides a match-like expression similar to cfg_if and based on the experimental cfg_match. The name switch is used to avoid conflict with the match keyword. Arms are evaluated top to bottom, and an optional wildcard arm can be provided if no match can be made.

An arm can either be:

  • a cfg(...) pattern (e.g., feature = "foo")
  • a wildcard _
  • an alias defined using define_alias

Common aliases are provided by cfg. Note that aliases are evaluated from the context of the defining crate, not the consumer.

ยงExamples

cfg::switch! {
    #[cfg(feature = "foo")] => {
        foo("We have the `foo` feature!")
    }
    cfg::std => {
        extern crate std;
        std::println!("No `foo`, but we have `std`!");
    }
    _ => {
        log("Don't have `std` or `foo`");
    }
}