How Box and Vec put data on the heap, what lives on the stack instead, and how destructors free it
The compiled program has been loaded into memory, but no function has run yet. Nothing is on the stack and nothing is on the heap
1fn main() {2 let boxed = Box::new(5);3 let vector = vec![9, 8, 7];4 println!("{boxed} {vector:?}");5}How Box and Vec put data on the heap, what lives on the stack instead, and how destructors free it
Vec on the stack is three fields: ptr, len and capBox<T> for a sized T is a single pointer. Boxes of unsized types such as Box<[T]> or Box<dyn Trait> are fat pointers that also carry a length or a vtable pointer0x1234 and 0x2468 are made up. The real allocator picks any free spot, and the addresses change from run to runVec doesn't promise a growth strategy, only that push is amortised O(1)$cargorun// waiting for the first println!