Consider the following
Rust program: trait Pet { fn speak(&self); } struct Cat { name: String } impl Cat { fn new(name: String) -> Self { Cat { name } } } impl Pet for Cat { fn speak(&self) { println!("{} says Meow!", self.name); } } fn talk(pet: T) { pet.speak(); } fn main() { let pet = Cat::new(String::from("Simba")); talk(pet); } the Rust Compiler will
monomorphize the program's code at compile time into: // [...] // struct Cat, Cat's impl, the Pet impl for struct Cat, and the Pet trait remain the same. fn talk_cat(pet: Cat) { Cat::speak(&pet) } fn main() { let pet = Cat::new(String::from("Simba")); talk_cat(pet); // talk(pet) gets replaced with the more specialized talk_cat } == See also ==