cognitive-engineering-lab / rust-book

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

Diagrams in 4.1 describe code that causes fatal stack overflow without warning/ferris #130

Open declspecl opened 9 months ago

declspecl commented 9 months ago

URL to the section(s) of the book with this problem: https://rust-book.cs.brown.edu/ch04-01-what-is-ownership.html

Description of the problem: In the section "Boxes Live in the Heap", the diagrams show a code snippet:

let a = [0; 1_000_000];
let b = a;

and later

let a = Box::new([0; 1_000_000]);
let b = a;

which is also talked about much more in the chapter. This code causes a stack overflow for both the stack-based array version and box-based heap version because the rust compiler actually copies a stack based array and moves it into the heap (https://doc.rust-lang.org/beta/alloc/boxed/index.html#examples). There is no warning about this or Ferris image to indicate to the reader that this code doesn't actually work, and since neither the compiler nor rust-analyzer warns about this either, I think it would be beneficial to include it in the chapter itself.

Suggested fix: Either add a Ferris image for "This code panics!", write a "Note:" section explaining this, or change the values in the array to be under the smallest default stack size across operating systems (512kib on mac iirc). I understand this is kind of a nit-picky issue but new readers who are following the book interactively and are met with a runtime error will be very confused.

willcrichton commented 5 months ago

Can you confirm this still happens for you? I tried both snippets in both the Rust Playground and my Macbook, and neither caused a stack overflow.