A Box<T>
stores data on the heap rather than the stack. A pointer to that heap data remains on the stack. They are useful when:
Immutable or mutable borrow checking is done at compile time.
Example:
pub enum LinkedList {
Con(i32, Box<LinkedList>),
Nil,
}
use crate::LinkedList::{Con, Nil};
fn main() {
let list = Con(1,
Box::new(Con(2,
Box::new(Con(3, Box::new(Nil))))));
assert_eq!(1, match list {
Con(value, next) => value,
Nil => -1,
});
}