about summary refs log tree commit diff
path: root/src/libcore/option.rs
diff options
context:
space:
mode:
authorAriel Ben-Yehuda <arielb1@mail.tau.ac.il>2017-08-29 21:40:53 +0000
committerGitHub <noreply@github.com>2017-08-29 21:40:53 +0000
commit11e75fd1150daafb39e769d6df2db73062edef06 (patch)
tree290cbcfcb3e8aa2ec597b96764224ce32de4b4bc /src/libcore/option.rs
parent630e02f25be1e65b316857c5bd8022da0b96db40 (diff)
parent96182997dab6436b17d01d7333e068e683de7ba5 (diff)
downloadrust-11e75fd1150daafb39e769d6df2db73062edef06.tar.gz
rust-11e75fd1150daafb39e769d6df2db73062edef06.zip
Rollup merge of #43705 - panicbit:option_ref_mut_cloned, r=aturon
libcore: Implement cloned() for Option<&mut T>

None
Diffstat (limited to 'src/libcore/option.rs')
-rw-r--r--src/libcore/option.rs20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/libcore/option.rs b/src/libcore/option.rs
index aecf2ee9325..138e04c7737 100644
--- a/src/libcore/option.rs
+++ b/src/libcore/option.rs
@@ -774,6 +774,26 @@ impl<'a, T: Clone> Option<&'a T> {
     }
 }
 
+impl<'a, T: Clone> Option<&'a mut T> {
+    /// Maps an `Option<&mut T>` to an `Option<T>` by cloning the contents of the
+    /// option.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(option_ref_mut_cloned)]
+    /// let mut x = 12;
+    /// let opt_x = Some(&mut x);
+    /// assert_eq!(opt_x, Some(&mut 12));
+    /// let cloned = opt_x.cloned();
+    /// assert_eq!(cloned, Some(12));
+    /// ```
+    #[unstable(feature = "option_ref_mut_cloned", issue = "43738")]
+    pub fn cloned(self) -> Option<T> {
+        self.map(|t| t.clone())
+    }
+}
+
 impl<T: Default> Option<T> {
     /// Returns the contained value or a default
     ///