cognitive-engineering-lab / rust-book

The Rust Programming Language: Experimental Edition
https://rust-book.cs.brown.edu
Other
503 stars 82 forks source link

Upside-down stack #189

Open carlotrimarchi opened 1 month ago

carlotrimarchi commented 1 month ago

URL to the section(s) of the book with this problem:

Variables live in the stack

Description of the problem:

In the section above there is a diagram illustrating how the stack changes when executing the following code:

fn main() {
    let n = 5;
    let y = plus_one(n);
    println!("The value of y is: {y}");
}

fn plus_one(x: i32) -> i32 {
    x + 1
}

The stack is represented in 3 steps:

  1. call to main
  2. call to plus_one
  3. back to main

What I find confusing is that in the second step the stack looks like this:

main
plus_one

From what I know (and also what's described in the standard version of the Rust book) the stack works similarly to pile of plates: each function call is added on top of the last one, and the one on top is the one that will get executed next.

The diagrams here seem to contradict this.

Suggested fix:

Modify the diagrams so that function calls appear on top of the previous ones

willcrichton commented 6 days ago

This is designed to be consistent with how stacks are drawn in other areas of computer systems. For historical reasons, call stacks grow down rather than up.