Macro konst::result::unwrap_err_or_else
source · macro_rules! unwrap_err_or_else { ($res:expr, |$param:pat_param| $expr:expr $(,)?) => { ... }; ($opt:expr, | $($anything:tt)* ) => { ... }; ($res:expr, $function:expr $(,)?) => { ... }; }
Expand description
Returns the error in the Err
variant,
otherwise runs a closure/function with the value in the Ok
variant.
§Example
use konst::result;
// Necessary for type inference reasons.
type Res = Result<u32, u32>;
const ARR: &[u32] = &[
// You can use a closure-like syntax to run code when the Result argument is Ok.
// `return` inside the "closure" returns from the function where this macro is called.
result::unwrap_err_or_else!(Res::Ok(3), |x| x + 2),
result::unwrap_err_or_else!(Res::Err(8), |_| loop{}),
// You can also pass functions
result::unwrap_err_or_else!(Res::Ok(16), add_34),
result::unwrap_err_or_else!(Res::Err(55), add_34),
];
assert_eq!(ARR, &[5, 8, 50, 55]);
const fn add_34(n: u32) -> u32 {
n + 34
}