Complete the code to create a new struct instance using struct update syntax.
struct Point { x: i32, y: i32 }
let p1 = Point { x: 5, y: 10 };
let p2 = Point { x: [1], ..p1 };You need to provide a new value for x in p2. The struct update syntax copies the rest from p1.
Complete the code to create a new struct instance copying all fields except one.
struct Color { red: u8, green: u8, blue: u8 }
let c1 = Color { red: 255, green: 0, blue: 0 };
let c2 = Color { green: 128, ..[1] };c2 instead of the original c1.Color instead of an instance.The struct update syntax copies fields from an existing instance. Here, c1 is the source.
Fix the error in the struct update syntax by completing the code.
struct Rectangle { width: u32, height: u32 }
let rect1 = Rectangle { width: 30, height: 50 };
let rect2 = Rectangle { width: 40, height: [1] };To copy the height from rect1, you must use rect1.height. This fixes the error.
Fill both blanks to create a new struct instance with updated fields using struct update syntax.
struct Circle { radius: f64, color: String }
let c1 = Circle { radius: 5.0, color: String::from("red") };
let c2 = Circle { radius: [1], color: [2], ..c1 };String::from for the color string.The new circle c2 has radius 10.0 and color blue, copying other fields from c1.
Fill all three blanks to create a new struct instance with updated fields and struct update syntax.
struct Employee { name: String, age: u32, department: String }
let emp1 = Employee { name: String::from("Alice"), age: 30, department: String::from("HR") };
let emp2 = Employee { name: [1], age: [2], department: [3], ..emp1 };String::from for string fields.The new employee emp2 has name Bob, age 25, and department IT, copying other fields from emp1.
