Skip to content

Commit f2d7045

Browse files
author
Ariel Ben-Yehuda
authored
Rollup merge of rust-lang#43705 - panicbit:option_ref_mut_cloned, r=aturon
libcore: Implement cloned() for Option<&mut T> None
2 parents faf477a + 9618299 commit f2d7045

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/libcore/option.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,26 @@ impl<'a, T: Clone> Option<&'a T> {
774774
}
775775
}
776776

777+
impl<'a, T: Clone> Option<&'a mut T> {
778+
/// Maps an `Option<&mut T>` to an `Option<T>` by cloning the contents of the
779+
/// option.
780+
///
781+
/// # Examples
782+
///
783+
/// ```
784+
/// #![feature(option_ref_mut_cloned)]
785+
/// let mut x = 12;
786+
/// let opt_x = Some(&mut x);
787+
/// assert_eq!(opt_x, Some(&mut 12));
788+
/// let cloned = opt_x.cloned();
789+
/// assert_eq!(cloned, Some(12));
790+
/// ```
791+
#[unstable(feature = "option_ref_mut_cloned", issue = "43738")]
792+
pub fn cloned(self) -> Option<T> {
793+
self.map(|t| t.clone())
794+
}
795+
}
796+
777797
impl<T: Default> Option<T> {
778798
/// Returns the contained value or a default
779799
///

0 commit comments

Comments
 (0)