# Rust's std::mem::take() swaps out a value while leaving a default in its place

**Date:** 2025-12-05  
**Tags:** Rust, Performance, Ownership  
**URL:** https://kelexine.is-a.dev/til/rust-mem-take

---

TIL: Rust's std::mem::take() swaps out a value while leaving a default in its place. Clean way to move out of a borrowed context without cloning. Saves heap allocations when working around the borrow checker.


```rust
let value = std::mem::take(&mut self.data);
// Work with value (now owns it)
process(value);
// self.data now contains Default::default()
self.data = new_value;
```



**Related:** [https://doc.rust-lang.org/std/mem/fn.take.html](https://doc.rust-lang.org/std/mem/fn.take.html)


---

*This content is available at [kelexine.is-a.dev/til/rust-mem-take](https://kelexine.is-a.dev/til/rust-mem-take)*
