We can use trait objects in a very similar way to generic type parameters. From one point of view, these two functions do the same thing:
fn print_generic<T>(value: T) where T: Display {
println!("{}", value);
}
This might seem like it does the same thing as the previous code:
fn print_trait(value: &dyn Display) {
println!("{}", value);
}
The first has a generic type parameter with a trait bound, the second accepts a trait object, which means both of them can work with many different data types, as long as the type in question has the Display trait.
Underneath, though, they're very different. The generic function is used to generate a version of the function that is specialized for each data type that is passed to it, while the compiler is running. That means that when we call the function while the program...