std-method

Rust Cell::take

Extract a payload while replacing it with T::default(). It is convenient for draining a small pending value when the default is an appropriate empty state. Expected output (not executed): Some("invoice") None Why: Option defaults to None, so the second extraction is empty. Common mistake: Assuming every type has a Default implementation or that its default is always empty.

Formal Syntax & Structure

rust syntaxrust
cell.take() -> T

Working Example

Example Snippetrust
use std::cell::Cell;fn main() {    let pending = Cell::new(Some(String::from("invoice")));    println!("{:?}", pending.take());    println!("{:?}", pending.take());}
Rust Reference