struct Haha {
pub a: u32,
pub b: Vec<u32>,
}
let example = Haha {
a: 32,
b: vec![1],
};
let new_a = example.a;
let new_b = example.b;
My understanding is:
new_ais a copy ofexample.asoexamplestill ownsexample.a.new_bnow ownsexample.bsinceexample.bwas moved.
Does rust implicitly copy example.a because it has Copy trait? And since example.b, which is a Vec, does not implement Copy trait, ownership of example.b is moved rather than copied?
Your understanding is correct.
ais copied whilebis moved. You can confirm this by trying to access the two fields afterwards.This prints
32.example.ais still accessible because it was copied, not moved.Accessing
example.bfails to compile with the error message:Which confirms exactly what you said, that
example.bwas moved because it doesn't implement theCopytrait.