Macro konst::option::ok_or_else

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

A const equivalent of Option::ok_or_else

§Example

use konst::option;

const ARR: &[Result<u32, 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::ok_or_else!(Some(3), || loop{}),
    option::ok_or_else!(None, || 5),

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

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

const fn thirteen() -> u32 {
    13
}