Macro match_all::for_match [] [src]

macro_rules! for_match {
    ($var:ident in $val:expr, $($($p:pat)|+ => $b:expr),+) => { ... };
}

for_match

Provides the for_match! macro for rust

This macro combines the functionality of a for loop and a match statement. So it iterates through each element in the expression and calls match on it

Format

for_match_all!{ ident in arr,
    pat | pat ... => expr,
    ...
}
  • arr: an expression that has the .iter() method, this holds the values to iterate through
  • IfNoMatch: the expression after this is executed if none of the other patterns are matched. This branch is optional.
  • pat | pat ...: this is groupings of patterns that will be checked. If any of them match to value then their corresponding expression is executed. After checking a group of patterns then the next group is checked until all groups have been checked. If none match then the IfNoMatch expression will be executed.

Example One

let arr = [1, 2, 3, 4];

for_match!{x in arr,
    1 | 3 | 5 => println!("odd"),
    _ => println!("even")
}

This would print:

odd
even
odd
even