Rust primarily uses two memory regions: the Stack and the Heap. Understanding how these regions work helps in writing efficient and safe Rust programs.
The Stack is a region of memory that operates in a Last In, First Out (LIFO) manner. It is fast, automatically managed, and does not require manual memory allocation.
- Memory is allocated and deallocated automatically
- Fast access due to predictable memory layout
- Limited in size, ideal for small/fixed-size data
- Stores function calls, local variables, and arguments
fn main() {
let x: i32 = 10; // Stack-allocated
let y: i32 = 20; // Stack-allocated
println!("Sum: {}", x + y);
}