Cell
Cell wraps a value and allows getting or setting the value using only a shared reference to the Cell. However, it does not allow any references to the inner value. Since there are no references, borrowing rules cannot be broken.
// Copyright 2025 Google LLC
// SPDX-License-Identifier: Apache-2.0
use std::cell::Cell;
fn main() {
// Note that `cell` is NOT declared as mutable.
let cell = Cell::new(5);
cell.set(123);
dbg!(cell.get());
}
Cellis a simple means to ensure safety: it has asetmethod that takes&self. This needs no runtime check, but requires moving values, which can have its own cost.