macro_rules! unwrap_or_else {
($e:expr, |$($error:ident)? $(_)*| $orelse:expr ) => { ... };
}
Expand description
Equivalent to Result::unwrap_or_else
but allows returning from the enclosing function.
§Examples
§Early return
use const_format::unwrap_or_else;
const fn unwrap_square(number: Result<u32, u32>) -> u64 {
let n = unwrap_or_else!(number, |n| return n as u64 ) as u64;
n * n
}
assert_eq!(unwrap_square(Ok(10)), 100);
assert_eq!(unwrap_square(Ok(30)), 900);
assert_eq!(unwrap_square(Err(100)), 100);
§As unwrap_or
use const_format::{AsciiStr, unwrap_or_else};
const FOO: AsciiStr = unwrap_or_else!(AsciiStr::new(b"AB\x80"), |_| AsciiStr::empty() );
const BAR: AsciiStr = unwrap_or_else!(AsciiStr::new(b"bar"), |_| loop{} );
assert_eq!(FOO.as_str(), "");
assert_eq!(BAR.as_str(), "bar");