There is the following struct:
struct Data {
xs: Vec<i32>,
ys: Vec<i32>
}
At attempt to assign one vector of the struct to the other as follows leads to an error:
impl Data {
fn proc(&mut self) {
self.xs = self.ys;
}
}
The error is move occurs because self.ys has type Vec<i32>, which does not implement the Copy trait.
The struct is borrowed as mutable, why it is not possible to move from self.ys there?
>Solution :
Use std::mem::swap to swap the values in-place:
impl Data {
fn proc(&mut self) {
std::mem::swap(&mut self.xs, &mut self.ys);
}
}
self.xs = self.ys doesn’t work because Vec doesn’t implement Copy: you’d have to .clone() ys.