You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There's actually a lot more going on in the Struct Update Syntax example that's not explicitly explained or mentioned.
For example, user1 still exists because we provide new String values into the fields email and username when we update the struct (up to this point, the reader will understand that the String type is a complex data type with its value stored in the heap). The remaining fields 'active' and 'sign_in_count' are primitive types, and therefore they are simply copied over instead of moved make user1 still valid.
If we were to leave out the new email String in user2 and kept it as the same value from user1, this would be a move. Once a move happens, the previous name becomes invalid (user1.email). Therefore user1 would become invalid in this case due to a partial move.
tldr: the book doesn't mention that the syntax will consume the original struct unless the values moved implement the Copy trait.
I feel like this would be important to explain so that new rustaceans can understand what's happening.
Thank you for reading, have a wonderful day!
The text was updated successfully, but these errors were encountered:
Thank you for the kind words! I think I've made a change to the example and added a discussion of the move that addresses this issue. Thanks for the suggestion!
There's actually a lot more going on in the Struct Update Syntax example that's not explicitly explained or mentioned.
For example, user1 still exists because we provide new String values into the fields email and username when we update the struct (up to this point, the reader will understand that the String type is a complex data type with its value stored in the heap). The remaining fields 'active' and 'sign_in_count' are primitive types, and therefore they are simply copied over instead of moved make user1 still valid.
If we were to leave out the new email String in user2 and kept it as the same value from user1, this would be a move. Once a move happens, the previous name becomes invalid (user1.email). Therefore user1 would become invalid in this case due to a partial move.
tldr: the book doesn't mention that the syntax will consume the original struct unless the values moved implement the Copy trait.
I feel like this would be important to explain so that new rustaceans can understand what's happening.
Thank you for reading, have a wonderful day!
The text was updated successfully, but these errors were encountered: