From 51b29d618ae949cb31dfacb91959309582ed0715 Mon Sep 17 00:00:00 2001 From: panicbit Date: Mon, 7 Aug 2017 01:25:43 +0200 Subject: [PATCH] libcore: Implement cloned() for Option<&mut T> --- src/libcore/option.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/libcore/option.rs b/src/libcore/option.rs index ef41b6794105..5328877a64b7 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -774,6 +774,25 @@ impl<'a, T: Clone> Option<&'a T> { } } +impl<'a, T: Clone> Option<&'a mut T> { + /// Maps an `Option<&mut T>` to an `Option` by cloning the contents of the + /// option. + /// + /// # Examples + /// + /// ``` + /// 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 = "0")] + pub fn cloned(self) -> Option { + self.map(|t| t.clone()) + } +} + impl Option { /// Returns the contained value or a default ///