Skip to content

Commit 8ab2d15

Browse files
author
Clar Charr
committed
Add Option::xor method
1 parent b183bd0 commit 8ab2d15

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/libcore/option.rs

+36
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,42 @@ impl<T> Option<T> {
705705
}
706706
}
707707

708+
/// Returns [`Some`] if exactly one of `self`, `optb` is [`Some`], otherwise returns `None`.
709+
///
710+
/// [`Some`]: #variant.Some
711+
/// [`None`]: #variant.None
712+
///
713+
/// # Examples
714+
///
715+
/// ```
716+
/// #![feature(option_xor)]
717+
///
718+
/// let x = Some(2);
719+
/// let y: Option<u32> = None;
720+
/// assert_eq!(x.xor(y), Some(2));
721+
///
722+
/// let x: Option<u32> = None;
723+
/// let y = Some(2);
724+
/// assert_eq!(x.xor(y), Some(2));
725+
///
726+
/// let x = Some(2);
727+
/// let y = Some(2);
728+
/// assert_eq!(x.xor(y), None);
729+
///
730+
/// let x: Option<u32> = None;
731+
/// let y: Option<u32> = None;
732+
/// assert_eq!(x.xor(y), None);
733+
/// ```
734+
#[inline]
735+
#[unstable(feature = "option_xor", issue = "50512")]
736+
pub fn xor(self, optb: Option<T>) -> Option<T> {
737+
match (self, optb) {
738+
(Some(a), None) => Some(a),
739+
(None, Some(b)) => Some(b),
740+
_ => None,
741+
}
742+
}
743+
708744
/////////////////////////////////////////////////////////////////////////
709745
// Entry-like operations to insert if None and return a reference
710746
/////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)