Reputation: 261
I have a struct that has a Vec
field and cannot implement Copy
:
#[derive(Clone, Debug)]
struct MyStruct {
field: Vec<i32>,
}
impl MyStruct {
fn new () -> MyStruct {
MyStruct {
field: vec![1, 2],
}
}
fn overwrite(&mut self, strt: MyStruct) {
self.field = strt.field;
}
}
If I want to use it multiple times later in my code, I have to clone()
it every time:
fn main() {
let mut s = MyStruct::new();
let s2 = MyStruct::new();
s.overwrite(s2.clone());
println!("{:?}", s2);
}
This works fine, but is this the best way to accomplish this task? Are there any other, more idiomatic ways?
Upvotes: 2
Views: 1438
Reputation: 300139
Let's review our data handling story:
&
and not &mut
, in a limited manner during)In your case, you should be using borrowing here.
fn overwrite(&mut self, strt: &MyStruct) {
// ^
self.field = strt.field.clone();
}
fn main() {
let mut s = MyStruct::new();
let s2 = MyStruct::new();
s.overwrite(&s2);
// ^
println!("{:?}", s2);
}
For an in-depth treatment of the concept, read the Rust Book. And if you come from a garbage collected language background, the chapters 4.7 to 4.10 are REALLY a must read.
Upvotes: 6