Skip to content

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

@Manishearth

Description

@Manishearth

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions