I'm learning Rust. I have a puzzle that I want to be answered:
/// struct User
#[derive(Debug)]
struct User {
id: usize,
name: String,
age: u8,
}
fn main() {
let old_user = User {
id: 1001,
name: String::from("allen"),
age: 23,
};
let new_user = User {
id: 1002,
..old_user
};
// it will give a error because 'name' has been removed
println!("{:?}", old_user);
// but it works
println!("{}", old_user.id);
}
The entire expression uses the given values for the fields that were specified and moves or copies the remaining fields from the base expression.
src: Reference: functional update syntax