cognitive-engineering-lab / rust-book

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

Chapter 5.3, misleading diagram #196

Open electrolyte-orb opened 4 days ago

electrolyte-orb commented 4 days ago

URL to the section(s) of the book with this problem: https://rust-book.cs.brown.edu/ch05-03-method-syntax.html#good-moves-and-bad-moves

Description of the problem: In the linked section, there is this sentence:

In this program, we call set_to_max with two rectangles r1 and r2. self is a mutable reference to r1 and other is a move of r2. After calling self.max(other), the max method consumes ownership of both rectangles. When max returns, Rust deallocates both strings "r1" and "r2" in the heap. Notice the problem: at the location L2, self is supposed to be readable and writable. However, (self).name (actually r1.name) has been deallocated.

Actually at L2, shouldn't *self and other be dropped at the end of the Rectangle.max function? In the diagram, L2 shows that r1 is intact, just that the name is undefined.

Suggested fix: Grey out r1 too.