How to Use std::cell for Interior Mutability
Use std::cell::RefCell<T> to wrap data that needs to be mutated through an immutable reference by calling borrow_mut() to get a mutable reference at runtime.
use std::cell::RefCell;
fn main() {
let data = RefCell::new(5);
*data.borrow_mut() = 10;
println!("{}", *data.borrow());
}
The RefCell enforces Rust's borrowing rules at runtime instead of compile time, panicking if you violate them (e.g., holding multiple mutable borrows).