Skip to content

Commit

Permalink
libcore: Implement cloned() for Option<&mut T>
Browse files Browse the repository at this point in the history
  • Loading branch information
panicbit committed Aug 7, 2017
1 parent ba1d065 commit 51b29d6
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/libcore/option.rs
Expand Up @@ -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<T>` 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<T> {
self.map(|t| t.clone())
}
}

impl<T: Default> Option<T> {
/// Returns the contained value or a default
///
Expand Down

0 comments on commit 51b29d6

Please sign in to comment.