Macro konst::option::filter

source ·
macro_rules! filter {
    ($e:expr, |$param:pat_param| $v:expr $(,)?) => { ... };
    ($opt:expr, | $($anything:tt)* ) => { ... };
    ($e:expr, $function:path $(,)?) => { ... };
}
Expand description

A const equivalent of Option::filter

§Example

use konst::option;

const ARR: &[Option<u32>] = &[
    // You can use a closure-like syntax to pass code that filters the Some variant.
    // `return` inside the "closure" returns from the function where this macro is called.
    option::filter!(Some(0), |&x| x == 0),
    option::filter!(Some(1), |x| *x == 0),
    option::filter!(None, |_| loop{}),

    // You can also pass functions
    option::filter!(Some(3), is_odd),
    option::filter!(Some(4), is_odd),
    option::filter!(None, is_odd),
];

assert_eq!(ARR, &[Some(0), None, None, Some(3), None, None]);

const fn is_odd(x: &u32) -> bool {
    *x % 2 == 1
}