switch

Macro switch 

Source
macro_rules! switch {
    () => { ... };
    ({$($tt:tt)*}) => { ... };
    (
        not($($args:tt)*) => $output:tt
        $($arms:tt)*
    ) => { ... };
    (
        all() => $output:tt
        $($arms:tt)*
    ) => { ... };
    (
        all($op:ident($($cond:tt)*)) => $output:tt
        $($arms:tt)*
    ) => { ... };
    (
        all($op:ident($($cond:tt)*), $($rest:tt)*) => $output:tt
        $($arms:tt)*
    ) => { ... };
    (
        all(#[cfg($meta:meta)]) => $output:tt
        $($arms:tt)*
    ) => { ... };
    (
        all(#[cfg($meta:meta)], $($rest:tt)*) => $output:tt
        $($arms:tt)*
    ) => { ... };
    (
        all($cond:path) => $output:tt
        $($arms:tt)*
    ) => { ... };
    (
        all($cond:path, $($rest:tt)*) => $output:tt
        $($arms:tt)*
    ) => { ... };
    (
        any() => $output:tt
        $($arms:tt)*
    ) => { ... };
    (
        any($op:ident($($cond:tt)*)) => $output:tt
        $($arms:tt)*
    ) => { ... };
    (
        any($op:ident($($cond:tt)*), $($rest:tt)*) => $output:tt
        $($arms:tt)*
    ) => { ... };
    (
        any(#[cfg($meta:meta)]) => $output:tt
        $($arms:tt)*
    ) => { ... };
    (
        any(#[cfg($meta:meta)], $($rest:tt)*) => $output:tt
        $($arms:tt)*
    ) => { ... };
    (
        any($cond:path) => $output:tt
        $($arms:tt)*
    ) => { ... };
    (
        any($cond:path, $($rest:tt)*) => $output:tt
        $($arms:tt)*
    ) => { ... };
    (
        _ => { $($output:tt)* }
    ) => { ... };
    (
        _ => $output:tt
        $($arms:tt)+
    ) => { ... };
    (
        #[cfg($meta:meta)] => $output:tt
        $($arms:tt)*
    ) => { ... };
    (
        $cond:path => $output:tt
        $($arms: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 alias

Note that aliases are evaluated from the context of the defining crate, not the consumer. This allows a library to export aliases for use in consuming crates.

ยงExamples

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