| 1 | # take_mut |
| 2 | |
| 3 | This crate provides (at this time) a single function, `take()`. |
| 4 | |
| 5 | `take()` allows for taking `T` out of a `&mut T`, doing anything with it including consuming it, and producing another `T` to put back in the `&mut T`. |
| 6 | |
| 7 | During `take()`, if a panic occurs, the entire process will be exited, as there's no valid `T` to put back into the `&mut T`. |
| 8 | |
| 9 | Contrast with `std::mem::replace()`, which allows for putting a different `T` into a `&mut T`, but requiring the new `T` to be available before being able to consume the old `T`. |
| 10 | |
| 11 | # Example |
| 12 | ```rust |
| 13 | struct Foo; |
| 14 | let mut foo = Foo; |
| 15 | take_mut::take(&mut foo, |foo| { |
| 16 | // Can now consume the Foo, and provide a new value later |
| 17 | drop(foo); |
| 18 | // Do more stuff |
| 19 | Foo // Return new Foo from closure, which goes back into the &mut Foo |
| 20 | }); |
| 21 | ``` |