-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
Improve description of access to Struct instances when a field is moved #4207
Comments
Making it more clear which struct instance fields are/aren't accessible after one or more fields have been moved. rust-lang#4207
Thanks for reporting. I am not sure if the text of that section really have to be improved, but the following is really interesting:
I totally missed that when I read that book in October 2023. Actually, moving fields of a struct in an interesting topic in general, I never thought about that, I will have to verify that in my own Rust book. [EDIT] I just had a look at the book of the Brown university: https://rust-book.cs.brown.edu/ch05-01-defining-structs.html#creating-instances-from-other-instances-with-struct-update-syntax. That explanation is a bit different and more what I remember. So my guess would be that this behavior might have changed in the last 18 months? |
I'm new to Rust so can't comment from experience regarding the last 18 months. The blame on Chapter 05-01 shows this edit from 3yrs ago. In that edit the At any rate, as you say moving fields of structs, and understanding what is still valid in the enclosing struct instance is an "interesting" topic, and IMHO it's at least worth flagging the interestingness to newcomers. |
Sorry, I am also relative new to Rust (started in October 2023), and I am a bit confused about this topic of moving struct fields. I think I actually misunderstand it initially in the way that moving one field out of a struct would invalidate the whole struct, which seems to be wrong. I was once playing with a similar use case -- having a vector of strings, and moving one element into another variable. I think that was just an invalid operation. Moving struct fields is even more interesting. I will try to learn more about this topic and fix the description in my own book -- perhaps I should ask in the Rust forum. A related Forum post is https://users.rust-lang.org/t/invalidation-of-struct-after-moving-one-of-its-attributes/77939 but it gives not much new information. |
Chapter 5.1 has this paragraph:
As a newcomer, these three portions weren't easily reconcilable:
This may be read as "neither user1 itself nor any part of user1 is accessible."
That section doesn't describe behaviour in the context of structs (i.e. that those fields are still accessible).
This seems to contradict the first line above, that the whole over user1 is not able to be used
I propose that the book should explicitly call out that access to user1 and its fields is not straightforward. Direct access to the user1 instance is no longer valid as one or more of its fields have been moved; but that access to any of the fields that have not been moved (because they implement Copy) is still valid.
The text was updated successfully, but these errors were encountered: