Skip to content

Commit

Permalink
Clarify explanation in 4.2 quiz. Fixes #148
Browse files Browse the repository at this point in the history
  • Loading branch information
willcrichton committed Jan 7, 2024
1 parent 3bf4024 commit 45190ee
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion quizzes/ch04-02-references-sec3-safety.toml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ println!("{}", k);
context = """
As we saw earlier in the section, `v.push(n)` can cause `v` to reallocate its internal contents, invalidating any references to the elements of `v` on the heap.
Therefore calling `give_and_take(&v, 4)` will cause previously-created element references to point to invalid memory. The two programs that bind `let n = &v[0]`
before `give_and_take` are candidates for undefined behavior.
before `give_and_take` are candidates for undefined behavior. `let v2 = &v` is not a candidate because a reference to the container `v` is not actually invalidated
by mutating `v`.
The program that does `println!("{}", n)` will cause undefined behavior by reading the invalid memory. The program that does `println!("{}", k)` will not cause undefined
behavior, because it does not use the invalidated reference.
Expand Down

0 comments on commit 45190ee

Please sign in to comment.