Search⌘ K

Make a Copy

Explore how to handle copying data in Rust structs and the importance of ownership rules. Learn to avoid inefficient duplication by using techniques like name shadowing while recognizing the impact on performance and best practices.

We'll cover the following...

One simple way to fix this is to have two separate Fruit values that have the exact same data inside. Let’s see what that might look like:

Rust 1.40.0
fn main() {
let fruit1 = Fruit {
apples: 10,
bananas: 5,
};
count_fruit(fruit1);
let fruit2 = Fruit {
apples: 10,
bananas: 5,
};
let price = price_fruit(fruit2);
println!("I can make {} cents", price);
}

We make a fruit1 variable, and move its ...