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.
main
branch to see if this has already been fixed, in this file: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:
, 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 aftergive_and_take
function call. Andv2
, at this point could refers to undefined memory.Suggested fix:
Add an explanation for this option, that
v2
points tov
on stack rather than memory on heap.