std-type

Rust Box<T>

Choose Box when one owner needs an indirectly stored value. The default Box owns its allocation and releases it on drop. A zero-sized payload does not require allocation. Boxing is not a guarantee of better speed or a particular stack layout. Expected output (not executed): 135 Why: Dereferencing the mutable box gives access to the owned integer. Common mistake: Boxing every small value even when an ordinary local variable is sufficient.

Formal Syntax & Structure

rust syntaxrust
Box<T>

Working Example

Example Snippetrust
fn main() {    let mut budget = Box::new(120_u32);    *budget += 15;    println!("{}", *budget);}
Rust Reference