Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 5.3, misleading diagram #196

Open
electrolyte-orb opened this issue Jun 27, 2024 · 0 comments
Open

Chapter 5.3, misleading diagram #196

electrolyte-orb opened this issue Jun 27, 2024 · 0 comments

Comments

@electrolyte-orb
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant