// https://google.github.io/comprehensive-rust/smart-pointers/trait-objects.html struct Dog { name: String, age: i8, } struct Cat { lives: i8, } trait Pet { fn talk(&self) -> String; } impl Pet for Dog { fn talk(&self) -> String { format!("Woof, my name is {}!", self.name) } } impl Pet for Cat { fn talk(&self) -> String { String::from("Miau!") } } pub fn run() { println!("\ntrait_obj (Google Kurs comprehensive-rust)"); println!( "{} {}", std::mem::size_of::(), std::mem::size_of::() ); println!( "{} {}", std::mem::size_of::<&Dog>(), std::mem::size_of::<&Cat>() ); println!("{}", std::mem::size_of::<&dyn Pet>()); println!("{}", std::mem::size_of::>()); let pets: Vec> = vec![ Box::new(Cat { lives: 9 }), Box::new(Dog { name: String::from("Fido"), age: 5, }), ]; for pet in pets { println!("Hello, who are you? {}", pet.talk()); } }