168 lines
15 KiB
Rust
168 lines
15 KiB
Rust
/* Rust Concepts: https://sociocyber.site/rust_concepts_v2.html
|
|
Concept: Stack vs Heap
|
|
Usage: Save data in Stack (CPU's registers, L1, L2, L3,. caches) or in Heap (MEMory)
|
|
|
|
* Box() make a pointer to data in HEAP
|
|
* Primitive Types saved in STACK
|
|
|
|
Stack = fast, fixed-size, automatic cleanup.
|
|
Heap = flexible, dynamic size, manually managed (but Rust does it for you).
|
|
|
|
(Analogy)
|
|
Stack = your desk (fast, limited).
|
|
Heap = a warehouse (vast, slower to retrieve).
|
|
|
|
P.S. '_' in '_variable' means = hide compiler warnings: unused variable
|
|
|
|
|
|
*/
|
|
|
|
fn main() {
|
|
|
|
/*
|
|
┌─────────────────────────────────────┬─────────────────────────────────────┐
|
|
│ STACK │ HEAP │
|
|
├─────────────────────────────────────┼─────────────────────────────────────┤
|
|
│ │ │
|
|
│ number: i32 │ │
|
|
│ ┌──────────────┐ │ │
|
|
│ │ 40 │ │ │
|
|
│ └──────────────┘ │ │
|
|
│ │ │
|
|
│ │ │
|
|
└─────────────────────────────────────┴─────────────────────────────────────┘
|
|
*/
|
|
// primitive type
|
|
let _number: i32 = 40; // saved in stack
|
|
|
|
/*
|
|
┌─────────────────────────────────────┬─────────────────────────────────────┐
|
|
│ STACK │ HEAP │
|
|
├─────────────────────────────────────┼─────────────────────────────────────┤
|
|
│ ... │ ... │
|
|
│ │ │
|
|
│ string: String │ │
|
|
│ ┌──────────────────────────────┐ │ 0x1000: │
|
|
│ │ ptr: 0x1000 │ │ ┌──────────────────────────────────┤
|
|
│ │ len: 19 │───┼─►│ "This data in a heap" │
|
|
│ │ cap: 19 │ │ │ 19 bytes │
|
|
│ └──────────────────────────────┘ │ └──────────────────────────────────┤
|
|
│ │ │
|
|
│ │ │
|
|
└─────────────────────────────────────┴─────────────────────────────────────┘
|
|
*/
|
|
// non-primitive types saved in heap, like as structs and etc (about it in later lessons)
|
|
let _string: String = String::from("This data in a heap");
|
|
|
|
|
|
/*
|
|
┌─────────────────────────────────────┬─────────────────────────────────────┐
|
|
│ STACK │ HEAP │
|
|
├─────────────────────────────────────┼─────────────────────────────────────┤
|
|
│ ... │ ... │
|
|
│ │ │
|
|
│ │ │
|
|
│ number_in_heap: Box<i32> │ │
|
|
│ ┌──────────────────────────────┐ │ 0x2000: │
|
|
│ │ ptr: 0x2000 │ │ ┌──────────────────────────────────┤
|
|
│ └──────────────────────────────────┼─►│ 78 │
|
|
│ │ │ 4 bytes │
|
|
│ │ └──────────────────────────────────┤
|
|
│ │ │
|
|
└─────────────────────────────────────┴─────────────────────────────────────┘
|
|
*/
|
|
// What if i want save big array of numbers in heap (may we read a big files, get big data from server like files, images, video streams,.)
|
|
// let _numbers_array: [i32; 12] = [1,23,3,56,57,6,868,34,3,5,246,23];
|
|
// Use Box::new(variable)
|
|
// For specific cases use Box::<Type of variable>::new(variable)
|
|
let number_in_heap = Box::new(78); // i32, saved in heap
|
|
println!("number_in_heap = {}", number_in_heap); // easy to accsess.
|
|
|
|
|
|
/*
|
|
┌─────────────────────────────────────┬─────────────────────────────────────┐
|
|
│ STACK │ HEAP │
|
|
├─────────────────────────────────────┼─────────────────────────────────────┤
|
|
│ ... │ ... │
|
|
│ │ │
|
|
│ │ │
|
|
│ numbers_array_in_heap: │ │
|
|
│ Box<[i32; 10]> │ │
|
|
│ ┌──────────────────────────────┐ │ 0x3000: │
|
|
│ │ ptr: 0x3000 │ │ ┌──────────────────────────────────┤
|
|
│ └──────────────────────────────────┼─►│ [1][2][3][4][5][6][7][8][9][10] │
|
|
│ │ │ 40 bytes │
|
|
│ │ └──────────────────────────────────┤
|
|
│ │ │
|
|
└─────────────────────────────────────┴─────────────────────────────────────┘
|
|
i32 = 4 bytes
|
|
4 bytes * 10 elements = 40 bytes -> size of array,
|
|
size of heap-space 40 bytes => 40bytes * 8bit = 320 bits (1's and 0's)
|
|
*/
|
|
// not worry about this. In Later lessons we'll explore "arrays"
|
|
let numbers_array_in_heap = Box::<[i32; 10]>::new([1,2,3,4,5,6,7,8,9,10]); // saved in heap, Box means create a POINTER to heap-data,
|
|
// save ONLY A POINTER in stack, Data save in HEAP.
|
|
// Print all elements of array
|
|
// Need iter() - iterator, not worry about this. In Later lessons we'll explore "loops"
|
|
for number in numbers_array_in_heap.iter() {
|
|
println!("number in heap from array: {}", number);
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
Full map
|
|
┌─────────────────────────────────────────────────────────────────────────────┐
|
|
│ MEMORY BREAKDOWN │
|
|
├─────────────────────┬──────────────────┬──────────────────┬─────────────────┤
|
|
│ Variable │ Stack Size │ Heap Address │ Heap Size │
|
|
├─────────────────────┼──────────────────┼──────────────────┼─────────────────┤
|
|
│ number │ 4 bytes │ (none) │ 0 bytes │
|
|
│ string │ 24 bytes │ 0x1000 │ 19 bytes │
|
|
│ number_in_heap │ 8 bytes │ 0x2000 │ 4 bytes │
|
|
│ numbers_array... │ 8 bytes │ 0x3000 │ 40 bytes │
|
|
├─────────────────────┼──────────────────┼──────────────────┼─────────────────┤
|
|
│ TOTAL │ 44 bytes │ │ 63 bytes │
|
|
└─────────────────────┴──────────────────┴──────────────────┴─────────────────┘
|
|
|
|
Box() make pointer to data in HEAP
|
|
Primitive Types saved in STACK
|
|
┌─────────────────────────────────────┬─────────────────────────────────────┐
|
|
│ STACK │ HEAP │
|
|
├─────────────────────────────────────┼─────────────────────────────────────┤
|
|
│ │ │
|
|
│ number: i32 │ │
|
|
│ ┌──────────────┐ │ │
|
|
│ │ 40 │ │ │
|
|
│ └──────────────┘ │ │
|
|
│ │ │
|
|
│ │ │
|
|
│ string: String │ │
|
|
│ ┌──────────────────────────────┐ │ 0x1000: │
|
|
│ │ ptr: 0x1000 │ │ ┌──────────────────────────────────┤
|
|
│ │ len: 19 │───┼─►│ "This data in a heap" │
|
|
│ │ cap: 19 │ │ │ 19 bytes │
|
|
│ └──────────────────────────────┘ │ └──────────────────────────────────┤
|
|
│ │ │
|
|
│ │ │
|
|
│ number_in_heap: Box<i32> │ │
|
|
│ ┌──────────────────────────────┐ │ 0x2000: │
|
|
│ │ ptr: 0x2000 │ │ ┌──────────────────────────────────┤
|
|
│ └──────────────────────────────────┼─►│ 78 │
|
|
│ │ │ 4 bytes │
|
|
│ │ └──────────────────────────────────┤
|
|
│ │ │
|
|
│ │ │
|
|
│ numbers_array_in_heap: │ │
|
|
│ Box<[i32; 10]> │ │
|
|
│ ┌──────────────────────────────┐ │ 0x3000: │
|
|
│ │ ptr: 0x3000 │ │ ┌──────────────────────────────────┤
|
|
│ └──────────────────────────────────┼─►│ [1][2][3][4][5][6][7][8][9][10] │
|
|
│ │ │ 40 bytes │
|
|
│ │ └──────────────────────────────────┤
|
|
│ │ │
|
|
└─────────────────────────────────────┴─────────────────────────────────────┘
|
|
|
|
|
|
*/
|
|
} // all freed automatically (stack, heap data drops)
|