#033
Mar 21, 2026
33. std::mem::take
Ever tried to move a value out of a &mut reference? The borrow checker won’t let you — but std::mem::take will. It swaps the value out and leaves Default::default() in its place.
| |
This is especially useful when working with enum state machines where you need to consume the current state:
| |
Without mem::take, you’d need .clone() or unsafe gymnastics to get the value out. See also mem::replace for when you want to specify what to leave behind instead of using Default.