The stack, the heap, pointers, and references
The stack is very fast although the heap isn't slow slow
The stack always needs the data that goes on it to have a defined size.
Rust needs to know the size of a variable at compile time so data that do not have a specified size or have dynamic sizes will end up on the heap and their pointers are stored on the stack because we always know the size of a pointer e.g i32
will always be 4 bytes
I think of pointers like signposts or like table contents that tell where you can find stuff but they aren't the real stuff. Pointers in Rust are referred to as references, A reference is denoted with an ampersand($
) in front of it e.g
let my_variable = 24;
let my_reference = $my_variable;
// This is a reference to data held by my_variable