Macro konst::option::unwrap_or_else

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

A const equivalent of Option::unwrap_or_else

§Example

use konst::option;

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

    // You can also pass functions
    option::unwrap_or_else!(Some(8), thirteen),
    option::unwrap_or_else!(None, thirteen),
];

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

const fn thirteen() -> u32 {
    13
}