cognitive-engineering-lab / rust-book

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

Additional Explanation for Q3 of last quiz in "References and Borrowing" #148

Closed hare0319 closed 5 months ago

hare0319 commented 6 months ago

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

Description of the problem:

The question 3 of the last quiz has a candidate answer:

let v = vec![1, 2, 3];
let v2 = &v;
give_and_take(&v, 4);
println!("{}", v2[0]);

, which couldn't cause undefined behaviour under assumptions of the question. But there's no explanation related to this answer. When I work on this question first time, I thought this could cause undefined behaviour, as v2[0] is accessed after give_and_take function call. And v2, at this point could refers to undefined memory.

Suggested fix:

Add an explanation for this option, that v2 points to v on stack rather than memory on heap.