Enum EitherOrBoth
pub enum EitherOrBoth<A, B = A>
Value that either holds a single A or B, or both.
Variants
-
Both(A, B) Both values are present.
-
Left(A) Only the left value of type
Ais present.-
Right(B) Only the right value of type
Bis present.
Implementations
impl<A, B> EitherOrBoth<A, B>
fn has_left(&self) -> boolIf
Left, orBoth, return true. Otherwise, return false.fn has_right(&self) -> boolIf
Right, orBoth, return true, otherwise, return false.fn is_left(&self) -> boolIf
Left, return true. Otherwise, return false. Exclusive version ofhas_left.fn is_right(&self) -> boolIf
Right, return true. Otherwise, return false. Exclusive version ofhas_right.fn is_both(&self) -> boolIf
Both, return true. Otherwise, return false.fn left(self) -> Option<A>If
Left, orBoth, returnSomewith the left value. Otherwise, returnNone.fn right(self) -> Option<B>If
Right, orBoth, returnSomewith the right value. Otherwise, returnNone.fn left_and_right(self) -> (Option<A>, Option<B>)Return tuple of options corresponding to the left and right value respectively
If
Leftreturn(Some(..), None), ifRightreturn(None,Some(..)), else return(Some(..),Some(..))fn just_left(self) -> Option<A>If
Left, returnSomewith the left value. IfRightorBoth, returnNone.Examples
// On the `Left` variant. # use ; let x: = Left; assert_eq!; // On the `Right` variant. let x: = Right; assert_eq!; // On the `Both` variant. let x = Both; assert_eq!;fn just_right(self) -> Option<B>If
Right, returnSomewith the right value. IfLeftorBoth, returnNone.Examples
// On the `Left` variant. # use ; let x: = Left; assert_eq!; // On the `Right` variant. let x: = Right; assert_eq!; // On the `Both` variant. let x = Both; assert_eq!;fn both(self) -> Option<(A, B)>If
Both, returnSomecontaining the left and right values. Otherwise, returnNone.fn into_left(self) -> A where B: Into<A>,If
LeftorBoth, return the left value. Otherwise, convert the right value and return it.fn into_right(self) -> B where A: Into<B>,If
RightorBoth, return the right value. Otherwise, convert the left value and return it.fn as_ref(&self) -> EitherOrBoth<&A, &B>Converts from
&EitherOrBoth<A, B>toEitherOrBoth<&A, &B>.fn as_mut(&mut self) -> EitherOrBoth<&mut A, &mut B>Converts from
&mut EitherOrBoth<A, B>toEitherOrBoth<&mut A, &mut B>.fn as_deref(&self) -> EitherOrBoth<&A::Target, &B::Target> where A: Deref, B: Deref,Converts from
&EitherOrBoth<A, B>toEitherOrBoth<&_, &_>using theDereftrait.fn as_deref_mut(&mut self) -> EitherOrBoth<&mut A::Target, &mut B::Target> where A: DerefMut, B: DerefMut,Converts from
&mut EitherOrBoth<A, B>toEitherOrBoth<&mut _, &mut _>using theDerefMuttrait.fn flip(self) -> EitherOrBoth<B, A>Convert
EitherOrBoth<A, B>toEitherOrBoth<B, A>.fn map_left<F, M>(self, f: F) -> EitherOrBoth<M, B> where F: FnOnce(A) -> M,Apply the function
fon the valueainLeft(a)orBoth(a, b)variants. If it is present rewrapping the result inself's original variant.fn map_right<F, M>(self, f: F) -> EitherOrBoth<A, M> where F: FnOnce(B) -> M,Apply the function
fon the valuebinRight(b)orBoth(a, b)variants. If it is present rewrapping the result inself's original variant.fn map_any<F, L, G, R>(self, f: F, g: G) -> EitherOrBoth<L, R> where F: FnOnce(A) -> L, G: FnOnce(B) -> R,Apply the functions
fandgon the valueaandbrespectively; found inLeft(a),Right(b), orBoth(a, b)variants. The Result is rewrappedself's original variant.fn left_and_then<F, L>(self, f: F) -> EitherOrBoth<L, B> where F: FnOnce(A) -> EitherOrBoth<L, B>,Apply the function
fon the valueainLeft(a)orBoth(a, _)variants if it is present.fn right_and_then<F, R>(self, f: F) -> EitherOrBoth<A, R> where F: FnOnce(B) -> EitherOrBoth<A, R>,Apply the function
fon the valuebinRight(b)orBoth(_, b)variants if it is present.fn or(self, l: A, r: B) -> (A, B)Returns a tuple consisting of the
landrinBoth(l, r), if present. Otherwise, returns the wrapped value for the present element, and the supplied value for the other. The first (l) argument is used for a missingLeftvalue. The second (r) argument is used for a missingRightvalue.Arguments passed to
orare eagerly evaluated; if you are passing the result of a function call, it is recommended to useor_else, which is lazily evaluated.Examples
# use EitherOrBoth; assert_eq!; assert_eq!; assert_eq!;fn or_default(self) -> (A, B) where A: Default, B: Default,Returns a tuple consisting of the
landrinBoth(l, r), if present. Otherwise, returns the wrapped value for the present element, and thedefaultfor the other.fn or_else<L: FnOnce() -> A, R: FnOnce() -> B>(self, l: L, r: R) -> (A, B)Returns a tuple consisting of the
landrinBoth(l, r), if present. Otherwise, returns the wrapped value for the present element, and computes the missing value with the supplied closure. The first argument (l) is used for a missingLeftvalue. The second argument (r) is used for a missingRightvalue.Examples
# use EitherOrBoth; let k = 10; assert_eq!; assert_eq!; assert_eq!;fn left_or_insert(&mut self, val: A) -> &mut AReturns a mutable reference to the left value. If the left value is not present, it is replaced with
val.fn right_or_insert(&mut self, val: B) -> &mut BReturns a mutable reference to the right value. If the right value is not present, it is replaced with
val.fn left_or_insert_with<F>(&mut self, f: F) -> &mut A where F: FnOnce() -> A,If the left value is not present, replace it the value computed by the closure
f. Returns a mutable reference to the now-present left value.fn right_or_insert_with<F>(&mut self, f: F) -> &mut B where F: FnOnce() -> B,If the right value is not present, replace it the value computed by the closure
f. Returns a mutable reference to the now-present right value.fn insert_left(&mut self, val: A) -> &mut ASets the
leftvalue of this instance, and returns a mutable reference to it. Does not affect therightvalue.Examples
# use ; // Overwriting a pre-existing value. let mut either: = Left; assert_eq!; // Inserting a second value. let mut either = Right; assert_eq!; assert_eq!;fn insert_right(&mut self, val: B) -> &mut BSets the
rightvalue of this instance, and returns a mutable reference to it. Does not affect theleftvalue.Examples
# use ; // Overwriting a pre-existing value. let mut either: = Left; assert_eq!; // Inserting a second value. let mut either = Left; assert_eq!; assert_eq!;fn insert_both(&mut self, left: A, right: B) -> (&mut A, &mut B)Set
selftoBoth(..), containing the specified left and right values, and returns a mutable reference to those values.
impl<T> EitherOrBoth<T, T>
fn reduce<F>(self, f: F) -> T where F: FnOnce(T, T) -> T,Return either value of left, right, or apply a function
fto both values if both are present. The input function has to return the same type as both Right and Left carry.This function can be used to preferably extract the left resp. right value, but fall back to the other (i.e. right resp. left) if the preferred one is not present.
Examples
# use EitherOrBoth; assert_eq!; assert_eq!; assert_eq!; // Extract the left value if present, fall back to the right otherwise. assert_eq!; assert_eq!; assert_eq!;
Trait Implementations
impl<A, B> From<Either<A, B>> for EitherOrBoth<A, B>
fn from(either: Either<A, B>) -> Self
impl<A: Clone, B: Clone> Clone for EitherOrBoth<A, B>
fn clone(&self) -> EitherOrBoth<A, B>
impl<A: Debug, B: Debug> Debug for EitherOrBoth<A, B>
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl<A: Eq, B: Eq> Eq for EitherOrBoth<A, B>
impl<A: Hash, B: Hash> Hash for EitherOrBoth<A, B>
fn hash<__H: Hasher>(&self, state: &mut __H)
impl<A: PartialEq, B: PartialEq> PartialEq for EitherOrBoth<A, B>
fn eq(&self, other: &EitherOrBoth<A, B>) -> bool
impl<A: PartialEq, B: PartialEq> StructuralPartialEq for EitherOrBoth<A, B>
Auto Trait Implementations
impl<A, B> Freeze for EitherOrBoth<A, B>
where
A: Freeze + Freeze,
B: Freeze + Freeze,
impl<A, B> RefUnwindSafe for EitherOrBoth<A, B>
where
A: RefUnwindSafe + RefUnwindSafe,
B: RefUnwindSafe + RefUnwindSafe,
impl<A, B> Send for EitherOrBoth<A, B>
where
A: Send + Send,
B: Send + Send,
impl<A, B> Sync for EitherOrBoth<A, B>
where
A: Sync + Sync,
B: Sync + Sync,
impl<A, B> Unpin for EitherOrBoth<A, B>
where
A: Unpin + Unpin,
B: Unpin + Unpin,
impl<A, B> UnsafeUnpin for EitherOrBoth<A, B>
where
A: UnsafeUnpin + UnsafeUnpin,
B: UnsafeUnpin + UnsafeUnpin,
impl<A, B> UnwindSafe for EitherOrBoth<A, B>
where
A: UnwindSafe + UnwindSafe,
B: UnwindSafe + UnwindSafe,
Blanket Implementations
impl<T> Any for EitherOrBoth<A, B>
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for EitherOrBoth<A, B>
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for EitherOrBoth<A, B>
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> CloneToUninit for EitherOrBoth<A, B>
where
T: Clone,
unsafe fn clone_to_uninit(&self, dest: *mut u8)
impl<T> From<T> for EitherOrBoth<A, B>
fn from(t: T) -> TReturns the argument unchanged.
impl<T> IntoEither for EitherOrBoth<A, B>
impl<T> ToOwned for EitherOrBoth<A, B>
where
T: Clone,
type Owned = T;fn to_owned(&self) -> Tfn clone_into(&self, target: &mut T)
impl<T, U> Into<U> for EitherOrBoth<A, B>
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for EitherOrBoth<A, B>
where
U: Into<T>,
type Error = never;fn try_from(value: U) -> Result<T, never>
impl<T, U> TryInto<U> for EitherOrBoth<A, B>
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>