When some non-
Copyfields from a struct is moved, but not all.
The variable binding the struct instance is still valid, but the fields that is moved is invalid, thus it’s a “partial” move.
We can still use fields that are not partially moved. Trying to use fields that have been partially moved will cause a compilation error.
Fields that are Copy is implicitly copied.
Recall
Copyis a marker trait that means bitwise copy is safe. Types that areCopyare automatically copied instead of moved.
struct Template {
cat: String,
dog: String
}
impl Default for Template {
fn default() -> Self {
Template {
cat: "Meow".to_string(),
dog: "Bark".to_string()
}
}
}
fn main() {
let template = Template::default();
let concrete = Template {
cat: "Nya".to_string(),
..template // template.dog is "partially" moved
};
println!("{}", template.cat); // Valid
println!("{}, {}", concrete.cat, concrete.dog); // valid
// NOTE: Invalid. template.cat is partially moved to concrete.cat
// println!("{}", template.cat);
}