Box
Box is a Rust owning smart pointer that provides indirection by storing a uniquely owned value in heap-allocated memory.
Learn Rust in a Month of Lunches
Box is Rust's owning smart pointer that stores a value on the heap while keeping a fixed-size pointer on the stack.
- How it is used
- Uses an owning heap pointer to make recursive structs and trait-object returns possible when a value’s size cannot be known directly.
- How deep it goes
- Works through construction, dereferencing, recursive-type compiler feedback, trait objects, multiple errors, and downcasting; does not develop linked-list construction.
- Examples
- Shows a boxed integer, a recursive struct corrected after a compiler error, a boxed trait return, and functions returning multiple error types.
- What you can do after
- Apply it via Rust’s `Box::new` to construct recursive values or return `Box<dyn Error>`, then try downcasting when the concrete error matters.
Rust in Action: Systems programming concepts and techniques
Box is Rust's owning smart pointer type for storing a value on the heap while keeping a pointer to it in the containing value.
- How it is used
- Uses Box to place values on the heap and to return trait-object errors from operations with different error types.
- How deep it goes
- Explains allocation, stack pointers, dereferencing, explicit drop, and why boxed trait objects have a known stack size.
- Examples
- Works through boxed integer addition, allocation and drop of boxed numbers, and a boxed error return type.
- What you can do after
- Apply it via Rust's Box::new to allocate a value, dereference it, and use Box<dyn Error> for differing errors.
Read how Rust in Action: Systems programming concepts and techniques explains Box →
Code Like a Pro in Rust
Box is a Rust smart-pointer type that provides unique ownership of a value allocated on the heap.
- How it is used
- Allocates a single value on the heap, including values held in linked-list nodes.
- How deep it goes
- Explains allocation and deallocation, then implements boxed linked-list links; leaves allocation-failure handling outside the worked example.
- Examples
- Builds a singly linked list with boxed data and optional boxed successors, then traverses its items.
- What you can do after
- Build it from scratch: represent and traverse a singly linked list using Box and Option.
Idiomatic Rust
Box is a Rust standard-library owning pointer that stores a value on the heap and provides indirection.
- How it is used
- Uses Box for heap allocation without shared ownership and for placing unsized trait objects in sized containers.
- How deep it goes
- Explains pointer choice, trait-object coercion, and redundant nested allocation through focused code examples.
- Examples
- Demonstrates Vec<Box<dyn MyTrait>> for mixed implementations and contrasts Vec<Box<String>> with Vec<String>.
- What you can do after
- Apply Box to heap-allocate single-owner values, contain trait objects, and avoid unnecessary boxing inside Vec.
Related concepts
- smart pointerkind of · A smart pointer is a pointer-like object that manages or mediates access to a referenced resource, often providing ownership, lifetime, or safety behavior.
- trait objectused by · A Rust trait object is a value accessed through a trait interface with its concrete implementing type hidden and methods dispatched dynamically.
- referencekind of · A reference is an indirect value or relationship that identifies or provides access to another value, object, resource, or source.
- vectoralternative to · A vector is an ordered, indexed sequence of elements, often numerical, represented as a mathematical quantity or a computational collection.
- memory allocationuses · Memory allocation is the process of reserving or assigning storage for program data and objects during execution.
- recursive data structureaddresses · A recursive data structure is a structure whose definition directly or indirectly refers to another instance of the same structure.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.



