853 B
853 B
Ownership
The Stack and the Heap
The stack is fast memory which follows the Last in First out (LIFO) principle.
On the stack, only data with a know size can live. If it can shrink or grow, then the data must be allocated to the heap.
When data is allocated to the heap, it gets a pointer to an address in memory.
Keeping track of these things are really important in languges like C. Other programming languages like Go use a garbage collector (GC)
which cleans up data in memory on a regular basis.
Rust does neither. It uses Ownership to manage memory and data, so each piece of data has an owner, and when that owner gets out of scope, the value is dropped.
Ownership rules
- Each value in Rust has an owner.
- There can only be one owner at a time.
- When the owner goes out of scope, the value will be dropped.