6 个版本
使用旧的 Rust 2015
| 0.2.2 | 2018 年 3 月 20 日 |
|---|---|
| 0.2.1 | 2018 年 3 月 15 日 |
| 0.2.0 | 2017 年 8 月 6 日 |
| 0.1.3 | 2016 年 2 月 22 日 |
在 Rust 模式 中排名 2496
每月下载量 422,246
在 952 个 crate 中使用 (直接使用 36 个)
12KB
204 行
take_mut
此 crate 提供了(目前)一个函数,take()。
take() 允许从 &mut T 中取出 T,对其进行任何操作,包括消耗它,并生成另一个 T 放回 &mut T。
在 take() 期间,如果发生 panic,整个进程将被退出,因为没有有效的 T 可以放回 &mut T。
与 std::mem::replace() 相比,它允许将不同的 T 放入 &mut T,但要求在消耗旧的 T 之前新 T 必须可用。
示例
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:
此 crate 提供了处理 &mut T 的几个函数,包括 take()。
take() 允许从 &mut T 中取出 T,对其进行任何操作,包括消耗它,并生成另一个 T 放回 &mut T。
在执行take()时,如果发生panic,整个过程将会被中止,因为没有有效的T可以放回&mut T中。在继续panic之前,请使用take_or_recover()将&mut T替换为恢复值。
与 std::mem::replace() 相比,它允许将不同的 T 放入 &mut T,但要求在消耗旧的 T 之前新 T 必须可用。