konst::option

Macro map

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

A const equivalent of Option::map

ยงExample

use konst::option;

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

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

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

const fn double(x: u32) -> u32 {
    x * 2
}