Macro either::try_left

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

Macro for unwrapping the left side of an Either, which fails early with the opposite side. Can only be used in functions that return Either because of the early return of Right that it provides.

See also try_right! for its dual, which applies the same just to the right side.


use either::{Either, Left, Right};

fn twice(wrapper: Either<u32, &str>) -> Either<u32, &str> {
    let value = either::try_left!(wrapper);
    Left(value * 2)

fn main() {
    assert_eq!(twice(Left(2)), Left(4));
    assert_eq!(twice(Right("ups")), Right("ups"));