Function take_mut::take [] [src]

pub fn take<T, F>(mut_ref: &mut T, closure: F) where F: FnOnce(T) -> T

Allows use of a value pointed to by &mut T as though it was owned, as long as a T is made available afterwards.

The closure must return a valid T.

Important

Will exit the program (with status code -1) if the closure panics.

Example

struct Foo;
let mut foo = Foo;
take_mut::take(&mut foo, |foo| {
    // Can now consume the Foo, and provide a new value later
    drop(foo);
    // Do more stuff
    Foo // Return new Foo from closure, which goes back into the &mut Foo
});