Fourth exercise
Analyze and fix the following code by using the clone trait:
use std::fmt::Display; #[derive(Debug)] struct Person { name: String, age: i32 } impl Display for Person { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let representation = format!("{} is {} years old.", self.name, self.age); write!(f, "{}", representation) } } fn main() { let p1 = Person { name: String::from("Klaas"), age: 123 }; let p2 = p1; println!("{}", p1); println!("{:?}", p2); }
Hint
Implement the clone trait to clone p1, for example by adding Clone to the derive statement and changing the line let p2 = p1 to let p2 = p1.clone().