Haskell's polymorphism isn't the same as rust. Rust has two kinds of polymorphism.
Static dispatch:
fn show<T>(a: T) -> String
where T: Display
{
Display::fmt(a)
}
fn main() {
show(1_i32);
show(1.0_f32);
}
is (handwaves) first translated to a version with no generics
fn show_i32(a: i32) -> String {
i32::fmt(a)
}
fn show_f32(a: f32) -> String {
f32::fmt(a)
}
fn main() {
show_i32(1_i32);
show_f32(1.0_f32);
}
You can probably see how the more permutations of functions you need to generate the slower it gets.
Dynamic dispatch:
fn show(a: Box<dyn Display>) -> String {
Display::fmt(a)
}
fn main() {
show(1_i32);
show(1.0_f32);
}
This translates to (handwaves):
fn show(a: Box<dyn Display>) -> String {
let fmt_fn = lookup_fn(Display::fmt, typeid_of(a))
fmt_fn(a)
}
fn main() {
show(1_i32);
show(1.0_f32);
}
This is simple to compile, but you need to do some extra work at runtime.