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.


Will abort the program if the closure panics.


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