Skip to content

Commit

Permalink
Rollup merge of rust-lang#43705 - panicbit:option_ref_mut_cloned, r=a…
Browse files Browse the repository at this point in the history
…turon

libcore: Implement cloned() for Option<&mut T>

None
  • Loading branch information
Ariel Ben-Yehuda authored Aug 29, 2017
2 parents faf477a + 9618299 commit f2d7045
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/libcore/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand Down

0 comments on commit f2d7045

Please sign in to comment.