Enum EitherOrBoth
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: &Self) -> boolIf
Left, orBoth, return true. Otherwise, return false.fn has_right(self: &Self) -> boolIf
Right, orBoth, return true, otherwise, return false.fn is_left(self: &Self) -> boolIf
Left, return true. Otherwise, return false. Exclusive version ofhas_left.fn is_right(self: &Self) -> boolIf
Right, return true. Otherwise, return false. Exclusive version ofhas_right.fn is_both(self: &Self) -> boolIf
Both, return true. Otherwise, return false.fn left(self: Self) -> Option<A>If
Left, orBoth, returnSomewith the left value. Otherwise, returnNone.fn right(self: Self) -> Option<B>If
Right, orBoth, returnSomewith the right value. Otherwise, returnNone.fn left_and_right(self: 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: 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: 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: Self) -> Option<(A, B)>If
Both, returnSomecontaining the left and right values. Otherwise, returnNone.fn into_left(self: Self) -> A where B: Into<A>If
LeftorBoth, return the left value. Otherwise, convert the right value and return it.fn into_right(self: Self) -> B where A: Into<B>If
RightorBoth, return the right value. Otherwise, convert the left value and return it.fn as_ref(self: &Self) -> EitherOrBoth<&A, &B>Converts from
&EitherOrBoth<A, B>toEitherOrBoth<&A, &B>.fn as_mut(self: &mut Self) -> EitherOrBoth<&mut A, &mut B>Converts from
&mut EitherOrBoth<A, B>toEitherOrBoth<&mut A, &mut B>.fn as_deref(self: &Self) -> EitherOrBoth<&<A as >::Target, &<B as >::Target> where A: Deref, B: DerefConverts from
&EitherOrBoth<A, B>toEitherOrBoth<&_, &_>using theDereftrait.fn as_deref_mut(self: &mut Self) -> EitherOrBoth<&mut <A as >::Target, &mut <B as >::Target> where A: DerefMut, B: DerefMutConverts from
&mut EitherOrBoth<A, B>toEitherOrBoth<&mut _, &mut _>using theDerefMuttrait.fn flip(self: Self) -> EitherOrBoth<B, A>Convert
EitherOrBoth<A, B>toEitherOrBoth<B, A>.fn map_left<F, M>(self: Self, f: F) -> EitherOrBoth<M, B> where F: FnOnce(A) -> MApply 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: Self, f: F) -> EitherOrBoth<A, M> where F: FnOnce(B) -> MApply 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: Self, f: F, g: G) -> EitherOrBoth<L, R> where F: FnOnce(A) -> L, G: FnOnce(B) -> RApply 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: 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: 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: 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: Self) -> (A, B) where A: Default, B: DefaultReturns 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: 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(self: &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(self: &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>(self: &mut Self, f: F) -> &mut A where F: FnOnce() -> AIf 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>(self: &mut Self, f: F) -> &mut B where F: FnOnce() -> BIf 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(self: &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(self: &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(self: &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: Self, f: F) -> T where F: FnOnce(T, T) -> TReturn 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 preferrably 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!;
impl<A, B> Freeze for EitherOrBoth<A, B>
impl<A, B> From for EitherOrBoth<A, B>
fn from(either: Either<A, B>) -> Self
impl<A, B> RefUnwindSafe for EitherOrBoth<A, B>
impl<A, B> Send for EitherOrBoth<A, B>
impl<A, B> StructuralPartialEq for EitherOrBoth<A, B>
impl<A, B> Sync for EitherOrBoth<A, B>
impl<A, B> Unpin for EitherOrBoth<A, B>
impl<A, B> UnsafeUnpin for EitherOrBoth<A, B>
impl<A, B> UnwindSafe for EitherOrBoth<A, B>
impl<A: $crate::clone::Clone, B: $crate::clone::Clone> Clone for EitherOrBoth<A, B>
fn clone(self: &Self) -> EitherOrBoth<A, B>
impl<A: $crate::cmp::Eq, B: $crate::cmp::Eq> Eq for EitherOrBoth<A, B>
impl<A: $crate::cmp::PartialEq, B: $crate::cmp::PartialEq> PartialEq for EitherOrBoth<A, B>
fn eq(self: &Self, other: &EitherOrBoth<A, B>) -> bool
impl<A: $crate::fmt::Debug, B: $crate::fmt::Debug> Debug for EitherOrBoth<A, B>
fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result
impl<A: $crate::hash::Hash, B: $crate::hash::Hash> Hash for EitherOrBoth<A, B>
fn hash<__H: $crate::hash::Hasher>(self: &Self, state: &mut __H)
impl<T> Any for EitherOrBoth<A, B>
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for EitherOrBoth<A, B>
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for EitherOrBoth<A, B>
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> CloneToUninit for EitherOrBoth<A, B>
unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)
impl<T> From for EitherOrBoth<A, B>
fn from(t: T) -> TReturns the argument unchanged.
impl<T> ToOwned for EitherOrBoth<A, B>
fn to_owned(self: &Self) -> Tfn clone_into(self: &Self, target: &mut T)
impl<T, U> Into for EitherOrBoth<A, B>
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for EitherOrBoth<A, B>
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for EitherOrBoth<A, B>
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>