#[derive(Debug)]
struct Car {
company: String,
model: String,
cost: i32
}
fn main() {
let car1 = Car {
company: String::from("Ford"),
model: String::from("123xyz"),
cost: 50000
};
let car2 = Car {
model: String::from("987pqr"),
cost: 47000,
..car1
};
println!("{:#?}", car2);
println!("{:#?}", car1.company);
}
Here compiler correctly tells me:
println!("{:#?}", car1.company);
^^^^^^^^^^^^ value borrowed here after move
move occurs because `car1.company` has type `String`, which does not implement the `Copy` trait
I understand that String instance is on the heap and that is why it cannot be copied, ownership was moved to the car2.company. I can solve this use-case by something like this:
#[derive(Debug, Clone)]
struct Car {
company: String,
model: String,
cost: i32
}
fn main() {
let car1 = Car {
company: String::from("Ford"),
model: String::from("123xyz"),
cost: 50000
};
let car2 = Car {
model: String::from("987pqr"),
cost: 47000,
..car1.clone()
};
println!("{:#?}", car2);
println!("{:#?}", car1.company);
}
Question 1: Is there a better way to do this?
Now my problem with this is that if the struct had many fields clone would create a copy of the car1 instance of the struct just to use its values to be assigned to the car2 instance of struct which itself will store the values.
If x is the memory required to store an instance of the struct Car,
- car1: x
- car1.clone: x
- car2: x
3x memory required where technically only 2x memory should have been needed.
Question 2: After car2 is instantiated will car1.clone() go out of scope and hence relinquish the memory?
If yes: this method of clone would be fine as only temporary one extra x memory is needed.
If no: then is there a way to achieve this goal without needed the extra x memory or taking ownership of the binding itself.