Macro stdext::return_ok

source ·
macro_rules! return_ok {
    ($var:expr) => { ... };
    (inner $var:expr) => { ... };
}
Expand description

Checks whether supplied Result variable is Ok and if so, returns it.

If variant is an Err, macro evaluates to the contents of the Err variant.

This macro supports two forms:

  • return_ok!(Ok(42)); - will return Ok(42).
  • return_ok!(inner Ok(42)); - will return just 42.

§Examples


fn choose_one(left: Result<u8, ()>, right: Result<u8, ()>) -> Result<u8, ()> {
    return_ok!(left);
    return_ok!(right);
    Err(())
}

fn choose_one_inner(left: Result<u8, ()>, right: Result<u8, ()>) -> u8 {
    return_ok!(inner left);
    return_ok!(inner right);
    panic!("Both variables are bad")
}

assert_eq!(choose_one(Err(()), Ok(10)), Ok(10));
assert_eq!(choose_one_inner(Ok(1), Err(())), 1);