File: README.md

package info (click to toggle)
rust-take-mut 0.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 96 kB
  • sloc: makefile: 2
file content (21 lines) | stat: -rw-r--r-- 818 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# take_mut

This crate provides (at this time) a single function, `take()`.

`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`.

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`.

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`.

# Example
```rust
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
});
```