std-method

Rust Cell::get_mut

If the caller already has exclusive access to the cell, get_mut exposes normal mutable access to the contents. It does not need runtime borrow tracking. This can be simpler than replacing a whole collection. Expected output (not executed): [1, 2, 3] Why: The mutable cell binding permits exclusive access to its vector. Common mistake: Expecting get_mut to work through a shared reference to Cell.

Formal Syntax & Structure

rust syntaxrust
cell.get_mut() -> &mut T

Working Example

Example Snippetrust
use std::cell::Cell;fn main() {    let mut tasks = Cell::new(vec![1, 2]);    tasks.get_mut().push(3);    println!("{:?}", tasks.into_inner());}
Rust Reference