I've got a simple struct and two instances of it as below:
#[derive(Debug)]
struct User {
first: String,
last: String,
age: u32,
}
let u1 = User {
first: String::from("John"),
last: String::from("Doe"),
age: 22,
};
let u2 = User {
first: String::from("Mary"),
..u1
};
println!("user: {:#?}", u1);
Error message:
error[E0382]: borrow of moved value: `u1`
--> src/main.rs:20:29
|
15 | let u2 = User {
| ______________-
16 | | first: String::from("Mary"),
17 | | ..u1
18 | | };
| |_____- value moved here
19 |
20 | println!("user: {:#?}", u1);
| ^^ value borrowed here after partial move
|
= note: move occurs because `u1.last` has type `std::string::String`, which does not implement the `Copy` trait
I tried to revise it to ..&u1 hoping it will pass the borrow check so that I could spread in the base struct (u1) to u2, but to no avail, wondering is it even possible for what I wanted to do here?
I understand it's because u1.last is a String, hence needing to pass a reference, but I'm not sure how to make it work in this scenario.