cognitive-engineering-lab / rust-book

The Rust Programming Language: Experimental Edition
https://rust-book.cs.brown.edu
Other
592 stars 92 forks source link

5.1 Misleading description of what happens when Creating Instances from Other Instances #119

Open djlondon opened 1 year ago

djlondon commented 1 year ago

The sentence here describing what happens to user1 after it's used to created user2 is inaccurate:

In this example, we can no longer use user1 after creating user2 because the String in the username field of user1 was moved into user2

This suggests that the entire user1 struct is now unusable, but it is not:

let user1 = User {
    email: String::from("someone@example.com"),
    username: String::from("someusername123"),
    active: true,
    sign_in_count: 1,
};

let user2 = User {
    email: String::from("another@example.com"),
    ..user1
};

println!("{} {}", user1.active, user1.email); // user1 is still usable

playground

I think a better description would be:

In this example, we can no longer use user1.username after creating user2 because the String in the username field of user1 was moved into user2

masterchess33 commented 1 month ago

Agree. The original sentence is misleading.