Box<T>
Box<T> owns heap-allocated T with single ownership - like Vec but single element.
Search across all documentation pages
Box<T> owns heap-allocated T with single ownership - like Vec but single element.
enum List { Cons(i32, Box<List>), Nil }
use List::{Cons, Nil};
fn main() {
let list = Cons(1, Box::new(Cons(2, Box::new(Nil))));
}When to reach for this: Recursive types, large values on stack avoidance, trait objects Box<dyn Trait>.
trait Draw { fn draw(&self); }
struct Circle;
impl Draw for Circle { fn draw(&self) { println!("circle"); } }
fn main() {
let shapes: Vec<Box<dyn Draw>> = vec![Box::new(Circle)];
for s in shapes { s.draw(); }
}What this demonstrates:
Box for indirectionBox<dyn Draw>Box implements Deref, Drop frees heap. Box<[T]> from Vec via into_boxed_slice. No refcount overhead.
*box or Box::into_inner (1.66+).alloc::boxed::Box with allocator.| Alternative | Use When | Don't Use When |
|---|---|---|
Vec | Dynamic array | Single value indirection |
Arc | Shared ownership | Single owner |
&T | Borrow | Need ownership transfer |
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