🦀 Rust Intermediate

What are Rust smart pointers?

Answer

Rust smart pointers add capabilities beyond regular references. Box<T> allocates a value on the heap; used for large data, recursive types, and trait objects. Rc<T> (Reference Counted) enables multiple ownership for single-threaded scenarios — a value is dropped when the last Rc clone is dropped. Arc<T> is the thread-safe equivalent of Rc using atomic reference counting — use it when sharing data across threads. RefCell<T> provides interior mutability: it enforces borrowing rules at runtime (not compile time), panicking if violated — useful with Rc for shared mutable data (Rc<RefCell<T>>). Mutex<T> and RwLock<T> provide interior mutability safe for multi-threaded use (combined with Arc).