Box
Box is an owned pointer to data on the heap:
1. fn main() {
2. let five = Box::new(5);
3. println!("five: {}", *five);
4. }

Box<T> implements Deref<Target = T>, which means that you can call methods from T directly on a Box.
Boxis likestd::unique_ptrin C++.- In the above example, you can even leave out the
*in theprintln!statement thanks toDeref. - A
Boxcan be useful when you: - have a type whose size that can’t be known at compile time, but the Rust compiler wants to know an exact size.
- want to transfer ownership of a large amount of data. To avoid copying large amounts of data on the stack, instead store the data on the heap in a
Boxso only the pointer is moved.
