In the book it says
Note that the struct update syntax uses
=
like an assignment; this is because it moves the data, just as we saw in the “Ways Variables and Data Interact: Move” section. In this example, we can no longer useuser1
after creatinguser2
because theString
in theusername
field ofuser1
was moved intouser2
.
but it just works
struct User {
active: bool,
username: String,
email: String,
sign_in_count: u64,
}
fn main() {
let user1 = User {
email: String::from("someone@example.com"),
username: String::from("someusername123"),
active: true,
sign_in_count: 1,
};
let user2 = User {
active: user1.active,
username: user1.username,
email: String::from("another@example.com"),
sign_in_count: user1.sign_in_count,
};
println!("{}, {}", user1.active, user2.active);
println!("{}", user1.email);
}
the only problem is that you cant access user1.username anymore.
why book says like that?