std-type
Rust UnsafeCell<T>
UnsafeCell is the foundational type for implementing interior-mutability abstractions. It relaxes shared-reference immutability for the enclosed value, not the exclusivity of mutable references or the prohibition on data races. This introduction uses only its safe exclusive-access API. Expected output (not executed): 6 Why: The ordinary mutable reference proves exclusive access; no raw-pointer dereference is needed. Common mistake: Assuming UnsafeCell makes all pointer aliasing or unsynchronized cross-thread mutation legal.
Formal Syntax & Structure
rust syntaxrust
UnsafeCell::new(value)Working Example
Example Snippetrust
use std::cell::UnsafeCell;fn main() { let mut counter = UnsafeCell::new(4); *counter.get_mut() += 2; println!("{}", counter.into_inner());}