Macro konst::option::or_else

source ·
macro_rules! or_else {
    ($opt:expr, || $mapper:expr $(,)? ) => { ... };
    ($opt:expr, | $($anything:tt)* ) => { ... };
    ($opt:expr, $function:path $(,)?) => { ... };
}
Expand description

A const equivalent of Option::or_else

§Example

use konst::option;

const ARR: &[Option<u32>] = &[
    // You can use a closure-like syntax to pass code that runs on None.
    // `return` inside the "closure" returns from the function where this macro is called.
    option::or_else!(Some(3), || loop{}),
    option::or_else!(None::<u32>, || Some(5)),

    // You can also pass functions
    option::or_else!(Some(8), thirteen),
    option::or_else!(None::<u32>, thirteen),
];

assert_eq!(ARR, &[Some(3), Some(5), Some(8), Some(13)]);

const fn thirteen() -> Option<u32> {
    Some(13)
}