The stack
Like most programming languages, Rust uses a stack to handle memory management for scopes. A stack is a simple data structure, also referred to as a Last In, First Out Queue or LIFO. Stacks support two operations: push, which stores a new value, and pop, which removes and returns the most recently stored value.
We can think of a stack as a pile of boxes. If we want to remove the stuff stored in the top box, we can just take it down and look inside. However, if we want to remove the stuff stored in one of the boxes underneath, we first have to remove the boxes above it. Here's a diagram of what I'm talking about, with access to the boxes underneath blocked by the ones above them:
When a Rust block expression starts, it makes a note ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access