diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index a922d4f118b..05c9c97a612 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -1023,6 +1023,27 @@ pub unsafe fn try_borrow_unguarded(&self) -> Result<&T, BorrowError> { } } +impl RefCell { + /// Takes the wrapped value, leaving `Default::default()` in its place. + /// + /// # Examples + /// + /// ``` + /// #![feature(refcell_take)] + /// use std::cell::RefCell; + /// + /// let c = RefCell::new(5); + /// let five = c.take(); + /// + /// assert_eq!(five, 5); + /// assert_eq!(c.into_inner(), 0); + /// ``` + #[unstable(feature = "refcell_take", issue = "71395")] + pub fn take(&self) -> T { + self.replace(Default::default()) + } +} + #[stable(feature = "rust1", since = "1.0.0")] unsafe impl Send for RefCell where T: Send {}