'How did I access the middle element of the stack if you can't access the middle element

I'm reading this document, and it says that The stack stores values in the order it gets them and removes the values in the opposite order. This is referred to as last in, first out. Think of a stack of plates: when you add more plates, you put them on top of the pile, and when you need a plate, you take one off the top. Adding or removing plates from the middle or bottom wouldn’t work as well!

So how does this work

fn main() {
    let _x = 1;
    let y = 2;
    let _z = 3;
    println!("{}", y);
}

Thanks for helping, sorry if I'm missing something obvious.



Solution 1:[1]

Every function needs a specific amount of RAM to hold all variables in this function. These are called frames. Frames are stacked, not the variables in them. Every time you call a function, a new frame is allocated. Every time you leave a function, the frame for this function is dropped.

The Function-Stack has nothing to do with accessing variables in the same frame.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 StackOverflower