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_leftandis_only_left, there isEither::is_leftandEither::has_left. - it doesn’t make sense to have an “only” version. For example, while there is
Either::map_left, there isn’tmap_only_left, because it would be unknown what theBothvariant’s left value should become when the left type changes.
Variants§
Implementations§
Source§impl<L, R> Either<L, R>
impl<L, R> Either<L, R>
Sourcepub fn from_options(left: Option<L>, right: Option<R>) -> MaybeEither<L, R>
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'));Sourcepub const fn as_ref(&self) -> Either<&L, &R>
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:?}");Sourcepub fn left(self) -> Option<L>
pub fn left(self) -> Option<L>
Gets the left value from either Left or Both. To exclude Both, use
Either::only_left.
Sourcepub fn only_left(self) -> Option<L>
pub fn only_left(self) -> Option<L>
Gets the left value from only Left. To include Both, use Either::left.
Sourcepub fn right(self) -> Option<R>
pub fn right(self) -> Option<R>
Gets the right value from either Right or Both. To exclude Both, use
Either::only_right.
Sourcepub fn only_right(self) -> Option<R>
pub fn only_right(self) -> Option<R>
Gets the right value from only Right. To include Both, use Either::right.
Sourcepub fn both(self) -> Option<(L, R)>
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);Sourcepub fn expect_left(self, message: &str) -> L
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");Sourcepub fn expect_only_left(self, message: &str) -> L
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");Sourcepub fn expect_right(self, message: &str) -> R
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");Sourcepub fn expect_only_right(self, message: &str) -> R
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");Sourcepub fn expect_both(self, message: &str) -> (L, R)
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");Sourcepub fn unwrap_left(self) -> L
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();Sourcepub fn unwrap_only_left(self) -> L
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();Sourcepub fn unwrap_right(self) -> R
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();Sourcepub fn unwrap_only_right(self) -> R
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();Sourcepub fn unwrap_both(self) -> (L, R)
pub fn unwrap_both(self) -> (L, R)
Sourcepub fn unwrap_left_or_default(self) -> Lwhere
L: Default,
pub fn unwrap_left_or_default(self) -> Lwhere
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.
Sourcepub fn unwrap_only_left_or_default(self) -> Lwhere
L: Default,
pub fn unwrap_only_left_or_default(self) -> Lwhere
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.
Sourcepub fn unwrap_right_or_default(self) -> Rwhere
R: Default,
pub fn unwrap_right_or_default(self) -> Rwhere
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.
Sourcepub fn unwrap_only_right_or_default(self) -> Rwhere
R: Default,
pub fn unwrap_only_right_or_default(self) -> Rwhere
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.
Sourcepub fn unwrap_both_or_default(self) -> (L, R)
pub fn unwrap_both_or_default(self) -> (L, R)
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"));Sourcepub fn unwrap_only_both_or_default(self) -> (L, R)
pub fn unwrap_only_both_or_default(self) -> (L, R)
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"));Sourcepub fn map<L2, R2, LF, RF>(self, left: LF, right: RF) -> Either<L2, R2>
pub fn map<L2, R2, LF, RF>(self, left: LF, right: RF) -> Either<L2, 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));Sourcepub fn map_left<L2, F>(self, f: F) -> Either<L2, R>where
F: FnOnce(L) -> L2,
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"));Sourcepub fn map_right<R2, F>(self, f: F) -> Either<L, R2>where
F: FnOnce(R) -> R2,
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));Sourcepub fn inspect<LF, RF>(self, left: LF, right: RF) -> Self
pub fn inspect<LF, RF>(self, left: LF, right: RF) -> Self
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");Sourcepub fn inspect_left<F>(self, f: F) -> Self
pub fn inspect_left<F>(self, f: F) -> Self
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");Sourcepub fn inspect_only_left<F>(self, f: F) -> Self
pub fn inspect_only_left<F>(self, f: F) -> Self
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");Sourcepub fn inspect_right<F>(self, f: F) -> Self
pub fn inspect_right<F>(self, f: F) -> Self
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");Sourcepub fn inspect_only_right<F>(self, f: F) -> Self
pub fn inspect_only_right<F>(self, f: F) -> Self
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");Sourcepub fn inspect_both<F>(self, f: F) -> Self
pub fn inspect_both<F>(self, f: F) -> Self
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");Sourcepub fn fill_left(self, left: L) -> Either<L, R>
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));Sourcepub fn fill_left_lazy<F>(self, f: F) -> Either<L, R>where
F: FnOnce() -> L,
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));Sourcepub fn fill_right(self, right: R) -> Either<L, R>
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));Sourcepub fn fill_right_lazy<F>(self, f: F) -> Either<L, R>where
F: FnOnce() -> R,
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));Sourcepub const fn is_left(&self) -> bool
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());Sourcepub const fn is_right(&self) -> bool
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());Sourcepub const fn has_left(&self) -> bool
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());Sourcepub const fn has_right(&self) -> bool
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());Sourcepub fn has_left_and<F>(&self, f: F) -> bool
pub fn has_left_and<F>(&self, f: F) -> 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));Sourcepub fn is_left_and<F>(&self, f: F) -> bool
pub fn is_left_and<F>(&self, f: F) -> 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));Sourcepub fn has_right_and<F>(&self, f: F) -> bool
pub fn has_right_and<F>(&self, f: F) -> 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));Sourcepub fn is_right_and<F>(&self, f: F) -> bool
pub fn is_right_and<F>(&self, f: F) -> 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));Sourcepub fn is_both_and<F>(&self, f: F) -> bool
pub fn is_both_and<F>(&self, f: F) -> 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));Sourcepub fn has_left_or<F>(&self, f: F) -> bool
pub fn has_left_or<F>(&self, f: F) -> bool
Returns true if a left value exists, or if the right value passes the predicate
f.
Sourcepub fn is_left_or<F>(&self, f: F) -> bool
pub fn is_left_or<F>(&self, f: F) -> bool
Returns true if the variant is Left, or if the right value passes the predicate
f.
Sourcepub fn has_right_or<F>(&self, f: F) -> bool
pub fn has_right_or<F>(&self, f: F) -> bool
Returns true if a right value exists, or if the left value passes the predicate
f.
Sourcepub fn is_right_or<F>(&self, f: F) -> bool
pub fn is_right_or<F>(&self, f: F) -> bool
Returns true if the variant is Right, or if the left value passes the predicate
f.
Sourcepub fn swap(self) -> Either<R, L>
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'));Sourcepub fn fold<T, F>(self, default_left: L, default_right: R, f: F) -> Twhere
F: FnOnce(L, R) -> T,
pub fn fold<T, F>(self, default_left: L, default_right: R, f: F) -> Twhere
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);Sourcepub fn fold_with<T, F, DLF, DRF>(
self,
default_left: DLF,
default_right: DRF,
f: F,
) -> T
pub fn fold_with<T, F, DLF, DRF>( self, default_left: DLF, default_right: DRF, f: F, ) -> 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.
§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>
impl<L, R> Either<&L, R>
pub fn left_copied(self) -> Either<L, R>where
L: Copy,
pub fn left_cloned(self) -> Either<L, R>where
L: Clone,
Source§impl<L, R> Either<L, &R>
impl<L, R> Either<L, &R>
pub fn right_copied(self) -> Either<L, R>where
R: Copy,
pub fn right_cloned(self) -> Either<L, R>where
R: Clone,
Source§impl<L, R> Either<&mut L, R>
impl<L, R> Either<&mut L, R>
pub fn left_copied(self) -> Either<L, R>where
L: Copy,
pub fn left_cloned(self) -> Either<L, R>where
L: Clone,
Source§impl<L, R> Either<L, &mut R>
impl<L, R> Either<L, &mut R>
pub fn right_copied(self) -> Either<L, R>where
R: Copy,
pub fn right_cloned(self) -> Either<L, R>where
R: Clone,
Source§impl<L, R> Either<Option<L>, Option<R>>
impl<L, R> Either<Option<L>, Option<R>>
Sourcepub fn transpose(self) -> MaybeEither<L, R>
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>>
impl<L, EL, R, ER> Either<Result<L, EL>, Result<R, ER>>
Sourcepub fn transpose(self, prefer_ok: bool) -> Result<Either<L, R>, Either<EL, ER>>
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>
impl<T> Either<T, T>
Sourcepub fn total(self) -> Twhere
T: Add<T, Output = T>,
pub fn total(self) -> Twhere
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);Sourcepub fn iter(&self) -> Iter<'_, T> ⓘ
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§
impl<L: Copy, R: Copy> Copy for Either<L, R>
impl<L: Eq, R: Eq> Eq for Either<L, R>
Source§impl<L, R> From<Either<L, R>> for Either<L, R>
impl<L, R> From<Either<L, R>> for Either<L, R>
Source§fn from(value: Other<L, R>) -> Self
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(_)));impl<L: PartialEq, R: PartialEq> StructuralPartialEq for Either<L, R>
Auto Trait Implementations§
impl<L, R> Freeze for Either<L, R>
impl<L, R> RefUnwindSafe for Either<L, R>where
L: RefUnwindSafe,
R: RefUnwindSafe,
impl<L, R> Send 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>where
L: UnsafeUnpin,
R: UnsafeUnpin,
impl<L, R> UnwindSafe for Either<L, R>where
L: UnwindSafe,
R: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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