Rust for beginners — Part 5 — Stack and Heap

Manikandan SP
3 min readFeb 27, 2022

--

In the previous section we looked into the primitive data types that are available in rust, next we will look into how Strings are used in rust

Note: Kindly have a look into the previous section of tutorial to understand about data types in rust

Previous section: https://medium.com/@manikandan96372/rust-for-beginners-part-4-data-types-a99f89da7101

Before jumping into Strings we must have a basic understanding on how stack and heap works and how that is utilized in rust.

Stack and Heap are the two different types of memory that makes the system memory to be available for your rust program during runtime. Let’s look into their implementation

STACK

Stack basically follows the LIFO approach (Last in First out). In rust, the variables whose required memory is of a known one or a fixed one then they are pushed into the stack one by one and cleared out of the memory in the opposite direction.

let A: i32 = 100;
let B: i8 = 2;
let C: i16 = 25;

In the above snippet we are declaring the variables A, B and C and assigning them with a particular value, while declaring it we know that it requires i32, i8 and i16 bits of memory and it is fixed, if we try to move above the memory limit, rust compiler would throw an error, so in this case we clearly know what is the memory requirement.

So stack is the right approach for memory allocation here as stack expects its storage to behave in the same manner.

i.e if variable A, B and C requires a fixed space in the memory and pushed into the stack in the order of A,B and C, once the execution completes the memory will be cleared in the order of C,B and A, because C gets popped out of the stack first and rest follows the same order.Inserting in between the stack or removing from the middle of the stack is not possible as stack does not support that kind of approach.

Ok, but integers are not the only type which we will be using in our program right ? There will be dynamic requirement where we will have to modify the memory used by the particular variables, for example “Strings”, you cannot determine a string to contain only certain set of characters, if user provides dynamic values during runtime it should be ready to handle them, but stack does not support this dynamic approach, so what would be the possible solution ?

Here comes ,

HEAP

Heap does not follow an organized approach like stack which uses LIFO, instead if you want to allocate memory using heap, your memory allocator will search the heap for a particular amount of space for your storage and returns the “address”, the right terminology used in rust is “pointer”, once we receive this pointer we will store that pointer in the stack

But wait…just now we saw that stack does not support this dynamic memory allocation approach but then how we will store it in stack ?

The thing to be noticed here is we are storing only the address in the stack which has a fixed size but the content of that still resides inside the heap.

In this way rust uses stack and heap for its memory allocation efficiently.

Now we have an understanding on how stack and heap are used in rust, let’s discuss about Strings in the next section.

--

--