Stack and Heap Memory
Stack
First in, first out. Faster. Some data-types are entirely stored in Stack Memory, e.g. integers, floating points, booleans, characters, etc.
| name | value |
|---|---|
| ptr | memory_address |
| len | 5 |
| capacity | 5 |
ptr: pointer to heap memory
len: length of memory used
capacity: memory allocated
Heap
Slower. Memory has to be allocated and Rust needs to keep track of where to find everything in the heap memory.
Some more complex data-types store “metadata” on the Stack Memory and its “content” on the Heap Memory, e.g. String.
| index | value |
|---|---|
| 0 | h |
| 1 | e |
| 2 | l |
| 3 | l |
| 4 | o |
Shallow vs Deep Copies
- Shallow —> only Stack memory is copied.
- Deep —> both Stack and Heap are copied.