Macro maybe_macro::maybe
[−]
[src]
macro_rules! maybe { ($i:ident if $e:expr) => { ... }; ($i:ident if let $p:pat = $e:expr) => { ... }; }
The maybe macro.
This macro takes a name and then an if
or if let
statement. If the
conditional is true or the pattern matches, the macro evaluates to Some
.
Otherwise, it evaluates to None
.
What the macro will evaluate - that comes before the if
statement - must
be an identifier, it can't be any expression.
Using with if
// This will filter out every value greater than 10. slice.iter().filter_map(|&x| maybe!(x if < 10))
Using with if let
// This will filter out all error values. slice.iter().filter_map(|x| maybe!(x if let Ok(_) = x));
Note that ident you return can also come from destructuring in the pattern:
slice.iter().filter_map(|x| maybe!(inner if let Ok(inner) = x));