All values in Rust are stack allocated by default. Values can be boxed (allocated in the heap) using the box construct. A box, with type signature Box<T>, is a smart pointer to a heap allocated value of type T. When a box goes out of scope, its destructor is called, the inner object is destroyed, and the memory in the heap is freed.

Boxed values can be dereferenced using the * operator, this removes one layer of indirection. Alternatively, the let box x = y pattern can be used to “unbox” y into x.

{box.play}