# [−][src]Function core::mem::take

    pub fn take<T: Default>(dest: &mut T) -> T

🔬 This is a nightly-only experimental API. (`mem_take` #61129)

Replace `dest` with the default value of `T`, and return the previous `dest` value.

# Examples

A simple example:

    #![feature(mem_take)]

    use std::mem;

    let mut v: Vec<i32> = vec![1, 2];

    let old_v = mem::take(&mut v);
    assert_eq!(vec![1, 2], old_v);
    assert!(v.is_empty());Run

`take` allows taking ownership of a struct field by replacing it with an "empty" value. Without `take` you can run into issues like these:

ⓘThis example deliberately fails to compile

    struct Buffer<T> { buf: Vec<T> }

    impl<T> Buffer<T> {
        fn get_and_reset(&mut self) -> Vec<T> {
            // error: cannot move out of dereference of `&mut`-pointer
            let buf = self.buf;
            self.buf = Vec::new();
            buf
        }
    }Run

Note that `T` does not necessarily implement `Clone`, so it can't even clone and reset `self.buf`. But `take` can be used to disassociate the original value of `self.buf` from `self`, allowing it to be returned:

    #![feature(mem_take)]

    use std::mem;

    impl<T> Buffer<T> {
        fn get_and_reset(&mut self) -> Vec<T> {
            mem::take(&mut self.buf)
        }
    }Run

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g., `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g., `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g., `str,u8` or `String,struct:Vec,test`)