Macro konst::option::and_then

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

A const equivalent of Option::and_then

§Example

use konst::option;

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

    // You can also pass functions
    option::and_then!(Some(23), checked_sub),
    option::and_then!(Some(9), checked_sub),
    option::and_then!(None::<u32>, checked_sub),
];

assert_eq!(ARR, &[Some(9), None, None, Some(13), None, None]);

const fn checked_sub(x: u32) -> Option<u32> {
    x.checked_sub(10)
}