Skip to content

Commit f121f09

Browse files
committed
Add RefCell::take
In the same vein as `Cell::take` and `Option::take`.
1 parent 019ab73 commit f121f09

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/libcore/cell.rs

+21
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,27 @@ impl<T: ?Sized> RefCell<T> {
10231023
}
10241024
}
10251025

1026+
impl<T: Default> RefCell<T> {
1027+
/// Takes the wrapped value, leaving `Default::default()` in its place.
1028+
///
1029+
/// # Examples
1030+
///
1031+
/// ```
1032+
/// #![feature(refcell_take)]
1033+
/// use std::cell::RefCell;
1034+
///
1035+
/// let c = RefCell::new(5);
1036+
/// let five = c.take();
1037+
///
1038+
/// assert_eq!(five, 5);
1039+
/// assert_eq!(c.into_inner(), 0);
1040+
/// ```
1041+
#[unstable(feature = "refcell_take", issue = "71395")]
1042+
pub fn take(&self) -> T {
1043+
self.replace(Default::default())
1044+
}
1045+
}
1046+
10261047
#[stable(feature = "rust1", since = "1.0.0")]
10271048
unsafe impl<T: ?Sized> Send for RefCell<T> where T: Send {}
10281049

0 commit comments

Comments
 (0)