Macro konst::try_opt

source ·
macro_rules! try_opt {
    ($opt:expr $(,)*) => { ... };
}
Expand description

?-like macro for Options.

§Example

use konst::try_opt;

const SOME: Option<u8> = sum_u8s(&[3, 5, 8, 13]);
assert_eq!(SOME, Some(29));

const NONE: Option<u8> = sum_u8s(&[3, 5, 8, 13, 240]);
assert_eq!(NONE, None);

const fn sum_u8s(mut nums: &[u8]) -> Option<u8> {
    let mut sum = 0_u8;
    while let [first, rem @ ..] = nums {
        nums = rem;
        sum = try_opt!(sum.checked_add(*first));
    }
    Some(sum)
}