Enum Either

enum Either<L, R>

Either represents an alternative holding one value out of either of the two possible values.

Either is a general purpose sum type of two parts. For representing success or error, use the regular Result<T, E> instead.

Variants

Left(L)

A value of type L.

Right(R)

A value of type R.

Implementations

impl<L, R> Either<L, R>

fn is_left(self: &Self) -> bool

Return true if the value is the Left variant.

use either::*;

let values = [Left(1), Right("the right value")];
assert_eq!(values[0].is_left(), true);
assert_eq!(values[1].is_left(), false);
fn is_right(self: &Self) -> bool

Return true if the value is the Right variant.

use either::*;

let values = [Left(1), Right("the right value")];
assert_eq!(values[0].is_right(), false);
assert_eq!(values[1].is_right(), true);
fn left(self: Self) -> Option<L>

Convert the left side of Either<L, R> to an Option<L>.

use either::*;

let left: Either<_, ()> = Left("some value");
assert_eq!(left.left(),  Some("some value"));

let right: Either<(), _> = Right(321);
assert_eq!(right.left(), None);
fn right(self: Self) -> Option<R>

Convert the right side of Either<L, R> to an Option<R>.

use either::*;

let left: Either<_, ()> = Left("some value");
assert_eq!(left.right(),  None);

let right: Either<(), _> = Right(321);
assert_eq!(right.right(), Some(321));
fn as_ref(self: &Self) -> Either<&L, &R>

Convert &Either<L, R> to Either<&L, &R>.

use either::*;

let left: Either<_, ()> = Left("some value");
assert_eq!(left.as_ref(), Left(&"some value"));

let right: Either<(), _> = Right("some value");
assert_eq!(right.as_ref(), Right(&"some value"));
fn as_mut(self: &mut Self) -> Either<&mut L, &mut R>

Convert &mut Either<L, R> to Either<&mut L, &mut R>.

use either::*;

fn mutate_left(value: &mut Either<u32, u32>) {
    if let Some(l) = value.as_mut().left() {
        *l = 999;
    }
}

let mut left = Left(123);
let mut right = Right(123);
mutate_left(&mut left);
mutate_left(&mut right);
assert_eq!(left, Left(999));
assert_eq!(right, Right(123));
fn flip(self: Self) -> Either<R, L>

Convert Either<L, R> to Either<R, L>.

use either::*;

let left: Either<_, ()> = Left(123);
assert_eq!(left.flip(), Right(123));

let right: Either<(), _> = Right("some value");
assert_eq!(right.flip(), Left("some value"));
fn map_left<F, M>(self: Self, f: F) -> Either<M, R>
where
    F: FnOnce(L) -> M

Apply the function f on the value in the Left variant if it is present.

use either::*;

let left: Either<_, u32> = Left(123);
assert_eq!(left.map_left(|x| x * 2), Left(246));

let right: Either<u32, _> = Right(123);
assert_eq!(right.map_left(|x| x * 2), Right(123));
fn map_right<F, S>(self: Self, f: F) -> Either<L, S>
where
    F: FnOnce(R) -> S

Apply the function f on the value in the Right variant if it is present.

use either::*;

let left: Either<_, u32> = Left(123);
assert_eq!(left.map_right(|x| x * 2), Left(123));

let right: Either<u32, _> = Right(123);
assert_eq!(right.map_right(|x| x * 2), Right(246));
fn either<F, G, T>(self: Self, f: F, g: G) -> T
where
    F: FnOnce(L) -> T,
    G: FnOnce(R) -> T

Apply one of two functions depending on contents, unifying their result. If the value is Left(L) then the first function f is applied; if it is Right(R) then the second function g is applied.

use either::*;

fn square(n: u32) -> i32 { (n * n) as i32 }
fn negate(n: i32) -> i32 { -n }

let left: Either<u32, i32> = Left(4);
assert_eq!(left.either(square, negate), 16);

let right: Either<u32, i32> = Right(-4);
assert_eq!(right.either(square, negate), 4);

impl<I> IntoIterator for Either<L, R>

fn into_iter(self: Self) -> I

impl<L, R> BufRead for Either<L, R>

fn fill_buf(self: &mut Self) -> Result<&[u8]>
fn consume(self: &mut Self, amt: usize)

impl<L, R> Deref for Either<L, R>

fn deref(self: &Self) -> &<Self as >::Target

impl<L, R> DerefMut for Either<L, R>

fn deref_mut(self: &mut Self) -> &mut <Self as >::Target

impl<L, R> Display for Either<L, R>

fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result

impl<L, R> DoubleEndedIterator for Either<L, R>

fn next_back(self: &mut Self) -> Option<<L as >::Item>

impl<L, R> Error for Either<L, R>

fn description(self: &Self) -> &str
fn cause(self: &Self) -> Option<&dyn Error>

impl<L, R> ExactSizeIterator for Either<L, R>

impl<L, R> Freeze for Either<L, R>

impl<L, R> From for Either<L, R>

fn from(r: Result<R, L>) -> Self

impl<L, R> Into for Either<L, R>

fn into(self: Self) -> Result<R, L>

impl<L, R> Iterator for Either<L, R>

fn next(self: &mut Self) -> Option<<L as >::Item>
fn size_hint(self: &Self) -> (usize, Option<usize>)

impl<L, R> Read for Either<L, R>

fn read(self: &mut Self, buf: &mut [u8]) -> Result<usize>
fn read_to_end(self: &mut Self, buf: &mut Vec<u8>) -> Result<usize>

impl<L, R> RefUnwindSafe for Either<L, R>

impl<L, R> Send for Either<L, R>

impl<L, R> StructuralPartialEq for Either<L, R>

impl<L, R> Sync for Either<L, R>

impl<L, R> Unpin for Either<L, R>

impl<L, R> UnsafeUnpin for Either<L, R>

impl<L, R> UnwindSafe for Either<L, R>

impl<L, R> Write for Either<L, R>

fn write(self: &mut Self, buf: &[u8]) -> Result<usize>
fn flush(self: &mut Self) -> Result<()>

impl<L, R, Target> AsMut for Either<L, R>

fn as_mut(self: &mut Self) -> &mut Target

impl<L, R, Target> AsRef for Either<L, R>

fn as_ref(self: &Self) -> &Target

impl<L: $crate::clone::Clone, R: $crate::clone::Clone> Clone for Either<L, R>

fn clone(self: &Self) -> Either<L, R>

impl<L: $crate::cmp::Eq, R: $crate::cmp::Eq> Eq for Either<L, R>

impl<L: $crate::cmp::Ord, R: $crate::cmp::Ord> Ord for Either<L, R>

fn cmp(self: &Self, other: &Either<L, R>) -> Ordering

impl<L: $crate::cmp::PartialEq, R: $crate::cmp::PartialEq> PartialEq for Either<L, R>

fn eq(self: &Self, other: &Either<L, R>) -> bool

impl<L: $crate::cmp::PartialOrd, R: $crate::cmp::PartialOrd> PartialOrd for Either<L, R>

fn partial_cmp(self: &Self, other: &Either<L, R>) -> Option<Ordering>

impl<L: $crate::fmt::Debug, R: $crate::fmt::Debug> Debug for Either<L, R>

fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result

impl<L: $crate::hash::Hash, R: $crate::hash::Hash> Hash for Either<L, R>

fn hash<__H: $crate::hash::Hasher>(self: &Self, state: &mut __H)

impl<L: $crate::marker::Copy, R: $crate::marker::Copy> Copy for Either<L, R>

impl<P, T> Receiver for Either<L, R>

impl<T> Any for Either<L, R>

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for Either<L, R>

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for Either<L, R>

fn borrow_mut(self: &mut Self) -> &mut T

impl<T> CloneToUninit for Either<L, R>

unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)

impl<T> From for Either<L, R>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for Either<L, R>

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T> ToString for Either<L, R>

fn to_string(self: &Self) -> String

impl<T, U> Into for Either<L, R>

fn into(self: Self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom for Either<L, R>

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto for Either<L, R>

fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>