Smart Pointers
Box
Owning a Heap Value
Box<T> stores a value on the heap while the box itself remains an ordinary owned value.
Program
Play the program to put a selected integer into a Box, dereference it, and compute from the boxed value.
box_heap_value.rs
fn main() {
let value = ;
let boxed = Box::new(value);
let doubled = *boxed * 2;
println!("{doubled}");
}
fn main() {
let value = ;
let boxed = Box::new(value);
let doubled = *boxed * 2;
println!("{doubled}");
}
fn main() {
let value = ;
let boxed = Box::new(value);
let doubled = *boxed * 2;
println!("{doubled}");
}
Box<T>
`Box<T>` owns one heap allocation containing a `T`.
dereference
`*boxed` reads the value stored inside the box.
ownership
When `boxed` goes out of scope, Rust drops the heap allocation.