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.1 Example with Struct Update Syntax is too surface level and needs more in depth explanation (esp for new rustaceans) #2789

Closed
hedgein opened this issue Jul 15, 2021 · 2 comments

Comments

@hedgein
Copy link

hedgein commented Jul 15, 2021

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!

@hedgein
Copy link
Author

hedgein commented Jul 28, 2021

Admire all the work you guys do btw :) !

@carols10cents
Copy link
Member

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!

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

No branches or pull requests

2 participants