What Is the Difference Between Cell<T> and RefCell<T>?

Cell<T> provides compile-time unchecked mutation while RefCell<T> enforces borrowing rules at runtime to prevent data races.

Cell allows mutation without borrowing checks at compile time but requires unsafe code for interior mutability, while RefCell enforces borrowing rules at runtime and panics if violated. Use Cell for simple, atomic-like swaps and RefCell for complex interior mutability where you need to borrow data.

use std::cell::{Cell, RefCell};

let c = Cell::new(5);
c.set(10); // No borrow, just set
println!("Cell: {}", c.get());

let r = RefCell::new(5);
let mut borrow = r.borrow_mut(); // Runtime borrow check
*borrow += 1;
println!("RefCell: {}", r.borrow());