Smart Pointers Basics
8 examples - 5 basic, 3 intermediate - for smart pointer types.
Search across all documentation pages
8 examples - 5 basic, 3 intermediate - for smart pointer types.
Box<T> Heap Allocationfn main() {
let b = Box::new(42);
println!("{}", *b);
}Single owner heap pointer.
Rc<T> Shared Ownershipuse std::rc::Rc;
let a = Rc::new(String::from("shared"));
let b = Rc::clone(&a);Reference counting - single thread.
Arc<T> Thread-Safe Shareduse std::sync::Arc;
let a = Arc::new(vec![1, 2]);Send + Sync atomic refcount.
RefCell<T> Interior Mutabilityuse std::cell::RefCell;
let c = RefCell::new(0);
*c.borrow_mut() += 1;Runtime borrow check - panics on violation.
Cow<'a, T> Clone-on-Writeuse std::borrow::Cow;
let s: Cow<str> = Cow::Borrowed("hello");Borrow until mutation forces owned copy.
Rc<RefCell<T>>use std::rc::{Rc, RefCell};
let v = Rc::new(RefCell::new(vec![1]));
v.borrow_mut().push(2);Graphs in single-threaded code.
Weak<T> Breaking Cyclesuse std::rc::{Rc, Weak};
// upgrade() returns Option<Rc<T>>Derefuse std::ops::Deref;
struct MyBox<T>(T);
impl<T> Deref for MyBox<T> {
type Target = T;
fn deref(&self) -> &T { &self.0 }
}Related: Smart Pointers Best Practices
Stack versions: This page was written for Rust 1.97.0 (edition 2024), Tokio 1.x, Axum 0.8, serde 1.0, sqlx 0.8, clap 4, and Polars 0.46+.
Reviewed by Chris St. John·Last updated Jul 16, 2026