macro_rules! or_do {
($v:expr, $do:expr) => { ... };
($v:expr, $err:ident => $do:expr) => { ... };
($v:expr, _ => $do:expr) => { ... };
}
Expand description
Unwrap an Option or Result or do something.
There is slightly different syntax for Options and Results. In options, you only need to specify the action. With Results you also need to specify the name of the error.
use ordoo::or_do;
let val: i32 = or_do!(Some(1), return);
let val: i32 = or_do!(Ok::<_, std::io::Error>(1), _ => return);
This simply expands to the following
let val: i32 = match Some(1) {
Some(v) => v,
None => return,
};
let val: i32 = match Ok::<_, std::io::Error>(1) {
Ok(v) => v,
Err(_) => return,
};
While this syntax is still a bit clunky, I find it much nicer than the alternative.