Skip to main content

Either

Enum Either 

Source
pub enum Either<L, R> {
    Left(L),
    Right(R),
    Both(L, R),
}
Expand description

The main enum, with variants Left, Right, and Both.

Because Both represents both left and right variants being present, most methods that act on either Left or Right will also act on Both. For example, Either::left should only return None on the variant Right, and Some on Left and Both. Methods that act exclusively on the Left or Right variants will usually have “only” in the name, like Either::only_left.

While most methods will have an “only” version, some will not if:

  • other wording would be more descriptive. For example, rather than define is_left and is_only_left, there is Either::is_left and Either::has_left.
  • it doesn’t make sense to have an “only” version. For example, while there is Either::map_left, there isn’t map_only_left, because it would be unknown what the Both variant’s left value should become when the left type changes.

Variants§

§

Left(L)

§

Right(R)

§

Both(L, R)

Both Left and Right variants, together.

Implementations§

Source§

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

Source

pub fn from_options(left: Option<L>, right: Option<R>) -> MaybeEither<L, R>

Converts two options to Option<Either<L, R>>.

§Example
assert!(Either::<(), ()>::from_options(None, None).is_none());
let left = Either::<_, ()>::from_options(Some(1), None).unwrap();
assert_eq!(left, Left(1));
let right = Either::<(), _>::from_options(None, Some(1)).unwrap();
assert_eq!(right, Right(1));
let both = Either::from_options(Some('l'), Some('r')).unwrap();
assert_eq!(both, Both('l', 'r'));
Source

pub const fn as_ref(&self) -> Either<&L, &R>

Converts &Either<L, R> to Either<&L, &R>

§Example
let value_and_label = Both(1usize, "one");
let threes: Either<usize, usize> = value_and_label
    .map_left(|value| value + 2)
    .map_right(|label| label.len());
println!("still can print value_and_label: {value_and_label:?}");
Source

pub fn left(self) -> Option<L>

Gets the left value from either Left or Both. To exclude Both, use Either::only_left.

Source

pub fn only_left(self) -> Option<L>

Gets the left value from only Left. To include Both, use Either::left.

Source

pub fn right(self) -> Option<R>

Gets the right value from either Right or Both. To exclude Both, use Either::only_right.

Source

pub fn only_right(self) -> Option<R>

Gets the right value from only Right. To include Both, use Either::right.

Source

pub fn both(self) -> Option<(L, R)>

Gets both values if they exist.

§Example
let vehicles = Both(2u8, 1u8);
let (cars, motorcycles) = vehicles
    .both()
    .expect("inventory for both cars and motorcycles to be tracked");
assert_eq!(cars, 2);
assert_eq!(motorcycles, 1);
Source

pub fn expect_left(self, message: &str) -> L

Tries to get a left value from either Left or Both. To exclude Both, use Either::expect_only_left.

§Examples
let left: Either<_, ()> = Left(1);
let one = left.expect_left("left to exist");
let right: Either<i32, _> = Right(());
let two = right.expect_left("left to exist");
let both = Both(3, ());
let three = both.expect_left("left to exist");
Source

pub fn expect_only_left(self, message: &str) -> L

Tries to get a left value from Left. To include Both, use Either::expect_left.

§Examples
let left: Either<_, ()> = Left(1);
let one = left.expect_only_left("left to exist");
let right: Either<i32, _> = Right(());
let two = right.expect_only_left("left to exist");
let both = Both(3, ());
let three = both.expect_only_left("left to exist");
Source

pub fn expect_right(self, message: &str) -> R

Tries to get a right value from either Right or Both. To exclude Both, use Either::expect_only_right.

§Examples
let left: Either<_, i32> = Left(());
let one = left.expect_right("left to exist");
let right: Either<(), _> = Right(2);
let two = right.expect_right("left to exist");
let both = Both((), 3);
let three = both.expect_right("left to exist");
Source

pub fn expect_only_right(self, message: &str) -> R

Tries to get a right value from Right. To include Both, use Either::expect_right.

§Examples
let left: Either<_, i32> = Left(());
let one = left.expect_only_right("left to exist");
let right: Either<(), _> = Right(2);
let two = right.expect_only_right("left to exist");
let both = Both((), 3);
let three = both.expect_only_right("left to exist");
Source

pub fn expect_both(self, message: &str) -> (L, R)

Tries to get both left and right values together.

§Examples
let left: Either<_, i32> = Left(1);
let (one, two) = left.expect_both("both to exist");
let right: Either<i32, _> = Right(4);
let (three, four) = right.expect_both("both to exist");
let both = Both(5, 6);
let (five, six) = both.expect_both("both to exist");
Source

pub fn unwrap_left(self) -> L

Tries to get a left value from either Left or Both. To exclude Both, use Either::unwrap_only_left.

§Examples
let left: Either<_, ()> = Left(1);
let one = left.unwrap_left();
let right: Either<i32, _> = Right(());
let two = right.unwrap_left();
let both = Both(3, ());
let three = both.unwrap_left();
Source

pub fn unwrap_only_left(self) -> L

Tries to get a left value from Left. To include Both, use Either::unwrap_left.

§Examples
let left: Either<_, ()> = Left(1);
let one = left.unwrap_only_left();
let right: Either<i32, _> = Right(());
let two = right.unwrap_only_left();
let both = Both(3, ());
let three = both.unwrap_only_left();
Source

pub fn unwrap_right(self) -> R

Tries to get a right value from either Right or Both. To exclude Both, use Either::unwrap_only_right.

§Examples
let left: Either<_, i32> = Left(());
let one = left.unwrap_right();
let right: Either<(), _> = Right(2);
let two = right.unwrap_right();
let both = Both((), 3);
let three = both.unwrap_right();
Source

pub fn unwrap_only_right(self) -> R

Tries to get a right value from Right. To include Both, use Either::unwrap_right.

§Examples
let left: Either<_, i32> = Left(());
let one = left.unwrap_only_right();
let right: Either<(), _> = Right(2);
let two = right.unwrap_only_right();
let both = Both((), 3);
let three = both.unwrap_only_right();
Source

pub fn unwrap_both(self) -> (L, R)

Tries to get both left and right values together.

§Examples
let left: Either<_, i32> = Left(1);
let (one, two) = left.unwrap_both();
let right: Either<i32, _> = Right(4);
let (three, four) = right.unwrap_both();
let both = Both(5, 6);
let (five, six) = both.unwrap_both();
Source

pub fn unwrap_left_or_default(self) -> L
where L: Default,

Tries to get the left value, or uses a default value. To unwrap only the Left variant, and exclude Both, use Either::unwrap_only_left_or_default.

Source

pub fn unwrap_only_left_or_default(self) -> L
where L: Default,

Tries to get the left from Left, or uses a default value. To also use the left value from Both, use Either::unwrap_left_or_default.

Source

pub fn unwrap_right_or_default(self) -> R
where R: Default,

Tries to get the right value, or uses a default value. To unwrap only the Right variant, and exclude Both, use Either::unwrap_only_right_or_default.

Source

pub fn unwrap_only_right_or_default(self) -> R
where R: Default,

Tries to get the right from Right, or uses a default value. To also use the right value from Both, use Either::unwrap_right_or_default.

Source

pub fn unwrap_both_or_default(self) -> (L, R)
where L: Default, R: Default,

Tries to get both the left and right values, and uses a default value for any missing value. To unwrap only Both, use Either::unwrap_only_both_or_default.

§Example
let left: Either<_, &str> = Left("left");
assert_eq!(left.unwrap_both_or_default(), ("left", ""));
let right: Either<&str, _> = Right("right");
assert_eq!(right.unwrap_both_or_default(), ("", "right"));
let both = Both("left", "right");
assert_eq!(both.unwrap_both_or_default(), ("left", "right"));
Source

pub fn unwrap_only_both_or_default(self) -> (L, R)
where L: Default, R: Default,

Tries to get both the left and right values. If the variant is not Both, the default value will be used for both L and R. To keep the existing value in a Left or Right variant, use Either::unwrap_both_or_default.

§Example
let left: Either<_, &str> = Left("left");
assert_eq!(left.unwrap_only_both_or_default(), ("", ""));
let right: Either<&str, _> = Right("right");
assert_eq!(right.unwrap_only_both_or_default(), ("", ""));
let both = Both("left", "right");
assert_eq!(both.unwrap_only_both_or_default(), ("left", "right"));
Source

pub fn map<L2, R2, LF, RF>(self, left: LF, right: RF) -> Either<L2, R2>
where LF: FnOnce(L) -> L2, RF: FnOnce(R) -> R2,

Maps Either<L, R> to Either<L2, R2> by applying a function to the left and right values.

§Example
let label: Either<_, i32> = Left("one");
let value: Either<&str, _> = Right(1);
let item = Both("one", 1);

assert_eq!(label.map(str::len, |n| n + 2), Left(3));
assert_eq!(value.map(str::len, |n| n + 2), Right(3));
assert_eq!(item.map(str::len, |n| n + 2), Both(3, 3));
Source

pub fn map_left<L2, F>(self, f: F) -> Either<L2, R>
where F: FnOnce(L) -> L2,

Maps Either<L, R> to Either<L2, R> by applying a function to Left or Both.

§Examples
let num_or_str: Either<_, &str> = Left(1usize);
let b: Either<bool, &str> = num_or_str.map_left(|num| num == 1);
assert_eq!(b, Left(true));
let num_or_str = Both(1usize, "false");
let b: Either<bool, &str> = num_or_str.map_left(|num| num == 1);
assert_eq!(b, Both(true, "false"));
Source

pub fn map_right<R2, F>(self, f: F) -> Either<L, R2>
where F: FnOnce(R) -> R2,

Maps Either<L, R> to Either<L2, R> by applying a function to Right or Both.

§Example
let num_or_str: Either<usize, _> = Right("false");
let b: Either<usize, bool> = num_or_str.map_right(|s| s == "true");
assert_eq!(b, Right(false));
let num_or_str = Both(1usize, "false");
let b: Either<usize, bool> = num_or_str.map_right(|s| s == "true");
assert_eq!(b, Both(1, false));
Source

pub fn inspect<LF, RF>(self, left: LF, right: RF) -> Self
where LF: FnOnce(&L), RF: FnOnce(&R),

Calls the left function on the left value and the right function on the right value. Returns the original Either.

§Example
let list = vec![1, 2, 3];
// NOTE from_options returns an Option<Either<L, R>>
let sum = Either::from_options(list.get(0), list.get(2))
    .and_then(|either| {
        either
            .inspect(|l| println!("left = {l}"), |r| println!("right = {r}"))
            .both()
    })
    .map(|(l, r)| l + r)
    .expect("list should have indices 0 and 2");
Source

pub fn inspect_left<F>(self, f: F) -> Self
where F: FnOnce(&L),

Calls a function with a reference to the left value. Returns the original Either.

To call the function on only the Left variant, use Either::inspect_only_left.

§Example
let list = vec![1, 2, 3];
// NOTE from_options returns an Option<Either<L, R>>
let first = Either::from_options(list.get(0), list.get(100))
    .and_then(|either| {
        either
            .inspect_left(|l| println!("left = {l}"))
            .left()
    })
    .expect("list should have index 0");
Source

pub fn inspect_only_left<F>(self, f: F) -> Self
where F: FnOnce(&L),

Calls a function with a reference to the value contained in only the Left variant. Returns the original Either.

To call the function on the Left or Both variant, use Either::inspect_left.

§Example
let list = vec![1, 2, 3];
// NOTE from_options returns an Option<Either<L, R>>
let first = Either::from_options(list.get(0), list.get(100))
    .and_then(|either| {
        either
            .inspect_only_left(|l| println!("left = {l}"))
            .only_left()
    })
    .expect("list should have index 0 and not index 100");
Source

pub fn inspect_right<F>(self, f: F) -> Self
where F: FnOnce(&R),

Calls a function with a reference to the right value. Returns the original Either.

To call the function on only the Right variant, use Either::inspect_only_right.

§Example
let list = vec![1, 2, 3];
// NOTE from_options returns an Option<Either<L, R>>
let last = Either::from_options(list.get(0), list.get(2))
    .and_then(|either| {
        either
            .inspect_right(|r| println!("right = {r}"))
            .right()
    })
    .expect("list should have index 2");
Source

pub fn inspect_only_right<F>(self, f: F) -> Self
where F: FnOnce(&R),

Calls a function with a reference to the value contained in only the Right variant. Returns the original Either.

To call the function on the Right or Both variant, use Either::inspect_right.

§Example
let list = vec![1, 2, 3];
// NOTE from_options returns an Option<Either<L, R>>
let last = Either::from_options(list.get(100), list.get(2))
    .and_then(|either| {
        either
            .inspect_only_right(|r| println!("right = {r}"))
            .only_right()
    })
    .expect("list shouldn't have index 100 but should have index 2");
Source

pub fn inspect_both<F>(self, f: F) -> Self
where F: FnOnce(&L, &R),

Calls a function with a reference to the left value and a reference to the right value contained in (and only in) Both.

If you would like to inspect all possible variants, you can chain Either::inspect_left and Either::inspect_right instead.

§Example
let list = vec![1, 2, 3];
// NOTE from_options returns an Option<Either<L, R>>
let (first, last) = Either::from_options(list.get(0), list.get(2))
    .and_then(|either| {
        either
            .inspect_both(|l, r| println!("left = {l}, right = {r}"))
            .both()
    })
    .expect("list should have indices 0 and 2");
Source

pub fn fill_left(self, left: L) -> Either<L, R>

If the left value doesn’t exist, it is populated.

§Example
let left: Either<u32, u64> = Left(1);
assert_eq!(left.fill_left(100), Left(1));
let right: Either<u32, u64> = Right(2);
assert_eq!(right.fill_left(100), Both(100, 2));
let both: Either<u32, u64> = Both(3, 4);
assert_eq!(both.fill_left(100), Both(3, 4));
Source

pub fn fill_left_lazy<F>(self, f: F) -> Either<L, R>
where F: FnOnce() -> L,

If the left value doesn’t exist, it is populated.

§Example
let left: Either<u32, u64> = Left(1);
assert_eq!(left.fill_left_lazy(|| 100), Left(1));
let right: Either<u32, u64> = Right(2);
assert_eq!(right.fill_left_lazy(|| 100), Both(100, 2));
let both: Either<u32, u64> = Both(3, 4);
assert_eq!(both.fill_left_lazy(|| 100), Both(3, 4));
Source

pub fn fill_right(self, right: R) -> Either<L, R>

If the right value doesn’t exist, it is populated.

§Example
let left: Either<u32, u64> = Left(1);
assert_eq!(left.fill_right(100), Both(1, 100));
let right: Either<u32, u64> = Right(2);
assert_eq!(right.fill_right(100), Right(2));
let both: Either<u32, u64> = Both(3, 4);
assert_eq!(both.fill_right(100), Both(3, 4));
Source

pub fn fill_right_lazy<F>(self, f: F) -> Either<L, R>
where F: FnOnce() -> R,

If the right value doesn’t exist, it is populated.

§Example
let left: Either<u32, u64> = Left(1);
assert_eq!(left.fill_right_lazy(|| 100), Both(1, 100));
let right: Either<u32, u64> = Right(2);
assert_eq!(right.fill_right_lazy(|| 100), Right(2));
let both: Either<u32, u64> = Both(3, 4);
assert_eq!(both.fill_right_lazy(|| 100), Both(3, 4));
Source

pub const fn is_left(&self) -> bool

Returns true if the variant is Left. To check if a left value exists (which includes Both) Either::has_left.

§Example
let left: Either<_, ()> = Left(());
assert!(left.is_left());
let both = Both((), ());
assert!(!both.is_left());
Source

pub const fn is_right(&self) -> bool

Returns true if the variant is Right. To check if a right value exists (which includes Both) Either::has_right.

let right: Either<(), _> = Right(());
assert!(right.is_right());
let both = Both((), ());
assert!(!both.is_right());
Source

pub const fn is_both(&self) -> bool

Returns true if the variant is Both.

Source

pub const fn has_left(&self) -> bool

Returns true if the variant is Left or Both. To check for only Left, use Either::is_left.

let left: Either<_, ()> = Left(());
assert!(left.has_left());
let both = Both((), ());
assert!(both.has_left());
Source

pub const fn has_right(&self) -> bool

Returns true if the variant is Right or Both. To check for only Right, use Either::is_right.

let right: Either<(), _> = Right(());
assert!(right.has_right());
let both = Both((), ());
assert!(both.has_right());
Source

pub fn has_left_and<F>(&self, f: F) -> bool
where F: FnOnce(&L) -> bool,

Returns true if a left value exists and f returns true. If you want to return false on Both, use Either::is_left_and.

§Example
let left: Either<_, ()> = Left(1);
assert!(left.has_left_and(|n| *n == 1));
let both = Both(1, ());
assert!(both.has_left_and(|n| *n == 1));
Source

pub fn is_left_and<F>(&self, f: F) -> bool
where F: FnOnce(&L) -> bool,

Returns true the variant is Left and f returns true. If you want to allow Both to return true, use Either::has_left_and.

§Example
let left: Either<_, ()> = Left(1);
assert!(left.is_left_and(|n| *n == 1));
let both = Both(1, ());
assert!(!both.is_left_and(|n| *n == 1));
Source

pub fn has_right_and<F>(&self, f: F) -> bool
where F: FnOnce(&R) -> bool,

Returns true if a right value exists and f returns true. If you want to return false on Both, use Either::is_right_and.

§Example
let right: Either<(), _> = Right(1);
assert!(right.has_right_and(|n| *n == 1));
let both = Both((), 1);
assert!(both.has_right_and(|n| *n == 1));
Source

pub fn is_right_and<F>(&self, f: F) -> bool
where F: FnOnce(&R) -> bool,

Returns true the variant is Right and f returns true. If you want to allow Both to return true, use Either::has_right_and.

§Example
let right: Either<(), _> = Right(1);
assert!(right.is_right_and(|n| *n == 1));
let both = Both((), 1);
assert!(!both.is_right_and(|n| *n == 1));
Source

pub fn is_both_and<F>(&self, f: F) -> bool
where F: FnOnce(&L, &R) -> bool,

Returns true if both left and right values exist and f returns true.

§Example
let both = Both(1, 2);
assert!(both.is_both_and(|a, b| a + b == 3));
Source

pub fn has_left_or<F>(&self, f: F) -> bool
where F: FnOnce(&R) -> bool,

Returns true if a left value exists, or if the right value passes the predicate f.

Source

pub fn is_left_or<F>(&self, f: F) -> bool
where F: FnOnce(&R) -> bool,

Returns true if the variant is Left, or if the right value passes the predicate f.

Source

pub fn has_right_or<F>(&self, f: F) -> bool
where F: FnOnce(&L) -> bool,

Returns true if a right value exists, or if the left value passes the predicate f.

Source

pub fn is_right_or<F>(&self, f: F) -> bool
where F: FnOnce(&L) -> bool,

Returns true if the variant is Right, or if the left value passes the predicate f.

Source

pub fn swap(self) -> Either<R, L>

Swaps the left and right values.

let left: Either<_, ()> = Left('l');
assert_eq!(left.swap(), Right('l'));
let right: Either<(), _> = Right('r');
assert_eq!(right.swap(), Left('r'));
let both = Both('l', 'r');
assert_eq!(both.swap(), Both('r', 'l'));
Source

pub fn fold<T, F>(self, default_left: L, default_right: R, f: F) -> T
where F: FnOnce(L, R) -> T,

Uses a function to fold the left and right values into each other. default_left and default_right are used where the left value or right value isn’t available.

If generating default_left or default_right is an expensive operation, consider using Either::fold_with.

§Example
let list = vec![1];

let left = Either::from_options(list.first().copied(), list.get(1).copied())
    .expect("list should have at least one element");
let sum = left.fold(100, 2, |l, r| l + r);
assert_eq!(sum, 1 + 2);
Source

pub fn fold_with<T, F, DLF, DRF>( self, default_left: DLF, default_right: DRF, f: F, ) -> T
where F: FnOnce(L, R) -> T, DLF: FnOnce() -> L, DRF: FnOnce() -> R,

Uses a function to fold the left and right values into each other. default_left and default_right are used where the left value or right value isn’t available.

§Example
fn expensive_function() -> i32 {
    100
}
let list = vec![1];

let left = Either::from_options(list.first().copied(), list.get(1).copied())
    .expect("list should have at least one element");
let sum = left.fold_with(
    expensive_function,
    expensive_function,
    |l, r| l + r,
);
assert_eq!(sum, 1 + 100);
Source§

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

Source

pub fn left_copied(self) -> Either<L, R>
where L: Copy,

Source

pub fn left_cloned(self) -> Either<L, R>
where L: Clone,

Source§

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

Source

pub fn right_copied(self) -> Either<L, R>
where R: Copy,

Source

pub fn right_cloned(self) -> Either<L, R>
where R: Clone,

Source§

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

Source

pub const fn copied(self) -> Either<L, R>
where L: Copy, R: Copy,

Source

pub fn cloned(self) -> Either<L, R>
where L: Clone, R: Clone,

Source§

impl<L, R> Either<&mut L, R>

Source

pub fn left_copied(self) -> Either<L, R>
where L: Copy,

Source

pub fn left_cloned(self) -> Either<L, R>
where L: Clone,

Source§

impl<L, R> Either<L, &mut R>

Source

pub fn right_copied(self) -> Either<L, R>
where R: Copy,

Source

pub fn right_cloned(self) -> Either<L, R>
where R: Clone,

Source§

impl<L, R> Either<&mut L, &mut R>

Source

pub const fn copied(self) -> Either<L, R>
where L: Copy, R: Copy,

Source

pub fn cloned(self) -> Either<L, R>
where L: Clone, R: Clone,

Source§

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

Source

pub fn transpose(self) -> MaybeEither<L, R>

Converts an Either<Option<L>, Option<R>> to an Option<Either<L, R>>.

§Example
let nothing: Either<Option<()>, Option<()>> = Both(None, None);
assert_eq!(nothing.transpose(), None);
let left: Either<_, Option<char>> = Left(Some('l'));
assert_eq!(left.transpose(), Some(Left('l')));
let left: Either<_, Option<char>> = Both(Some('l'), None);
assert_eq!(left.transpose(), Some(Left('l')));
let both = Both(Some('l'), Some('r'));
assert_eq!(both.transpose(), Some(Both('l', 'r')));
Source§

impl<L, EL, R, ER> Either<Result<L, EL>, Result<R, ER>>

Source

pub fn transpose(self, prefer_ok: bool) -> Result<Either<L, R>, Either<EL, ER>>

Converts an Either<Result<L, EL>, Result<R, ER>> to an Result<Either<L, R>, Either<EL, ER>>.

In the case of mixed results (Both(ok, err) or Both(err, ok)), Ok will be returned if prefer_ok is true, and Err will be returned if prefer_ok is false.

§Example
let ok_left: Either<Result<_, ()>, Result<(), ()>> = Left(Ok(1));
assert_eq!(ok_left.transpose(true), Ok(Left(1)));
let ok_both: Either<Result<_, ()>, Result<_, ()>> = Both(Ok(3), Ok(4));
assert_eq!(ok_both.transpose(true), Ok(Both(3, 4)));
let err_left: Either<Result<(), _>, Result<(), ()>> = Left(Err('a'));
assert_eq!(err_left.transpose(true), Err(Left('a')));
let err_both: Either<Result<(), _>, Result<(), _>> = Both(Err('b'), Err('c'));
assert_eq!(err_both.transpose(true), Err(Both('b', 'c')));

// The prefer_ok argument is used to decide what to do with mixed results.
let mixed_results: Either<Result<bool, _>, Result<_, ()>>
    = Both(Err("error"), Ok("success"));
assert_eq!(mixed_results.transpose(true), Ok(Right("success")));
let mixed_results: Either<Result<bool, _>, Result<_, ()>>
    = Both(Err("error"), Ok("success"));
assert_eq!(mixed_results.transpose(false), Err(Left("error")));
Source§

impl<T> Either<T, T>

Source

pub fn total(self) -> T
where T: Add<T, Output = T>,

Gets the total value of the left and/or right values. If only one value exists, it is returned. If both left and right values exist, they are added together.

§Example
use std::collections::HashMap;
let mut vehicles = HashMap::from([("car", 3usize)]);
let total = Either::from_options(vehicles.get("car").copied(), vehicles.get("truck").copied())
    .map(|either| either.total())
    .expect("vehicles should have at least one entry");
assert_eq!(total, 3);

vehicles.insert("truck", 2);
let total = Either::from_options(vehicles.get("car").copied(), vehicles.get("truck").copied())
    .map(|either| either.total())
    .expect("vehicles should have at least one entry");
assert_eq!(total, 3 + 2);
Source

pub fn iter(&self) -> Iter<'_, T>

Creates an iterator that iterates over one or two items. In the case of Both, the left value will always be the first item, and the right value will be the second.

§Examples
let either: Either<_, char> = Left('l');
let mut iter = either.iter();
assert_eq!(iter.next(), Some(&'l'));
assert_eq!(iter.next(), None);
let either: Either<char, _> = Right('r');
let mut iter = either.iter();
assert_eq!(iter.next(), Some(&'r'));
assert_eq!(iter.next(), None);
let either = Both('l', 'r');
let mut iter = either.iter();
assert_eq!(iter.next(), Some(&'l'));
assert_eq!(iter.next(), Some(&'r'));
assert_eq!(iter.next(), None);

Trait Implementations§

Source§

impl<L: Clone, R: Clone> Clone for Either<L, R>

Source§

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

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<L: Copy, R: Copy> Copy for Either<L, R>

Source§

impl<L: Debug, R: Debug> Debug for Either<L, R>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<L: Eq, R: Eq> Eq for Either<L, R>

Source§

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

Source§

fn from((left, right): (L, R)) -> Self

Converts a tuple of two values to Both.

§Example
let item = ("Dark mode", true);
let label_and_value = Either::from(item);
assert_eq!(label_and_value, Both("Dark mode", true));
Source§

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

Source§

fn from(value: Other<(L, R), Other<L, R>>) -> Self

Converts to this type from the input type.
Source§

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

Source§

fn from(value: Other<Other<L, R>, (L, R)>) -> Self

Converts to this type from the input type.
Source§

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

Source§

fn from(value: Other<L, R>) -> Self

Converts the Either variant from the either crate to this crate’s Either. This will either produce the variant Left or Right, but never Both.

§Example
let left: either::Either<_, ()> = either::Left(());
let right: either::Either<(), _> = either::Right(());
assert!(matches!(left.into(), either_both::Left(_)));
assert!(matches!(right.into(), either_both::Right(_)));
Source§

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

Source§

fn from(value: Either<L, R>) -> Self

Converts to this type from the input type.
Source§

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

Source§

fn from(value: Either<L, R>) -> Self

Converts to this type from the input type.
Source§

impl<L: Hash, R: Hash> Hash for Either<L, R>

Source§

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

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<L: PartialEq, R: PartialEq> PartialEq for Either<L, R>

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

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

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

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

§

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

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

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

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.