Skip to content

Commit 51b29d6

Browse files
committed
libcore: Implement cloned() for Option<&mut T>
1 parent ba1d065 commit 51b29d6

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/libcore/option.rs

+19
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,25 @@ 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+
/// let mut x = 12;
785+
/// let opt_x = Some(&mut x);
786+
/// assert_eq!(opt_x, Some(&mut 12));
787+
/// let cloned = opt_x.cloned();
788+
/// assert_eq!(cloned, Some(12));
789+
/// ```
790+
#[unstable(feature = "option_ref_mut_cloned", issue = "0")]
791+
pub fn cloned(self) -> Option<T> {
792+
self.map(|t| t.clone())
793+
}
794+
}
795+
777796
impl<T: Default> Option<T> {
778797
/// Returns the contained value or a default
779798
///

0 commit comments

Comments
 (0)