#mut #foo #temporarily #closures #return #now #consume

unmaintained take_mut

Take a T from a &mut T temporarily

6 releases

Uses old Rust 2015

0.2.2 Mar 20, 2018
0.2.1 Mar 15, 2018
0.2.0 Aug 6, 2017
0.1.3 Feb 22, 2016

#2315 in Rust patterns

Download history 137098/week @ 2024-11-17 126184/week @ 2024-11-24 150242/week @ 2024-12-01 147697/week @ 2024-12-08 137110/week @ 2024-12-15 49334/week @ 2024-12-22 60083/week @ 2024-12-29 139065/week @ 2025-01-05 171436/week @ 2025-01-12 137259/week @ 2025-01-19 147778/week @ 2025-01-26 168118/week @ 2025-02-02 156070/week @ 2025-02-09 155112/week @ 2025-02-16 170349/week @ 2025-02-23 165273/week @ 2025-03-02

655,779 downloads per month
Used in 1,040 crates (39 directly)

MIT license

12KB
204 lines

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

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
});

lib.rs:

This crate provides several functions for handling &mut T including 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 aborted, as there's no valid T to put back into the &mut T. Use take_or_recover() to replace the &mut T with a recovery value before continuing the panic.

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.

No runtime deps