Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upside-down stack #189

Open
carlotrimarchi opened this issue May 22, 2024 · 1 comment
Open

Upside-down stack #189

carlotrimarchi opened this issue May 22, 2024 · 1 comment

Comments

@carlotrimarchi
Copy link

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
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants