about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMark Simulacrum <mark.simulacrum@gmail.com>2018-05-17 13:51:20 -0600
committerGitHub <noreply@github.com>2018-05-17 13:51:20 -0600
commit0c0bb18a5b0675f2d7c64efb334ff564104174f4 (patch)
treed61626a15519130c94fcae746888c187a1e809c9
parent6e95b8715cc7eec3bb987a70698eb7bb1d93a67f (diff)
parent8ab2d15f6753054797c88f07028e4802c43b70ab (diff)
downloadrust-0c0bb18a5b0675f2d7c64efb334ff564104174f4.tar.gz
rust-0c0bb18a5b0675f2d7c64efb334ff564104174f4.zip
Rollup merge of #50553 - clarcharr:option_xor, r=sfackler
Add Option::xor method

Implements the method requested in #50512.
-rw-r--r--src/libcore/option.rs36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/libcore/option.rs b/src/libcore/option.rs
index 0dfdabee031..28f37f72d6f 100644
--- a/src/libcore/option.rs
+++ b/src/libcore/option.rs
@@ -705,6 +705,42 @@ impl<T> Option<T> {
         }
     }
 
+    /// Returns [`Some`] if exactly one of `self`, `optb` is [`Some`], otherwise returns `None`.
+    ///
+    /// [`Some`]: #variant.Some
+    /// [`None`]: #variant.None
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(option_xor)]
+    ///
+    /// let x = Some(2);
+    /// let y: Option<u32> = None;
+    /// assert_eq!(x.xor(y), Some(2));
+    ///
+    /// let x: Option<u32> = None;
+    /// let y = Some(2);
+    /// assert_eq!(x.xor(y), Some(2));
+    ///
+    /// let x = Some(2);
+    /// let y = Some(2);
+    /// assert_eq!(x.xor(y), None);
+    ///
+    /// let x: Option<u32> = None;
+    /// let y: Option<u32> = None;
+    /// assert_eq!(x.xor(y), None);
+    /// ```
+    #[inline]
+    #[unstable(feature = "option_xor", issue = "50512")]
+    pub fn xor(self, optb: Option<T>) -> Option<T> {
+        match (self, optb) {
+            (Some(a), None) => Some(a),
+            (None, Some(b)) => Some(b),
+            _ => None,
+        }
+    }
+
     /////////////////////////////////////////////////////////////////////////
     // Entry-like operations to insert if None and return a reference
     /////////////////////////////////////////////////////////////////////////