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

Struct update syntax example doesn't make it clear that order doesn't matter #1996

Closed
Manishearth opened this issue Jun 17, 2019 · 0 comments
Milestone

Comments

@Manishearth
Copy link
Member

https://doc.rust-lang.org/book/ch05-01-defining-structs.html#creating-instances-from-other-instances-with-struct-update-syntax

let user2 = User {
    email: String::from("[email protected]"),
    username: String::from("anotherusername567"),
    active: user1.active,
    sign_in_count: user1.sign_in_count,
};


let user2 = User {
    email: String::from("[email protected]"),
    username: String::from("anotherusername567"),
    ..user1
};

Someone working through this book asked me "should I be choosing to order my fields in the struct in a way that makes the most sense for struct update syntax?", which didn't quite make sense to me until I realized that the code gives the impression that struct update syntax can only fill in the last fields of a struct.

It may be worth picking different fields in the example, or explicitly clarifying this. I'm not sure how to do this with picking different fields without changing the motivation there, though.

Note: It was clear to this person that you don't need to specify fields in order for normal struct init syntax, it was specifically this context where they got confused.

@carols10cents carols10cents added this to the ch5 milestone Jul 17, 2021
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