Monomorphization
"Compile-time process where polymorphic functions are replaced by many monomorphic functions for each unique instantiation." -- Wikipedia
"The code that results from monomorphization is doing static dispatch, which is when the compiler knows what method you're calling at compile time." -- The Book
Before monomorphization
fn id<T>(x: T) -> T { x } fn main() { let int_id = id(10); let f32_id = id(1.1_f32); let string_id = id("hello"); println!("{}", int_id); println!("{}", f32_id); println!("{}", string_id); }
After Monomorphization
Rust might generate monomorphic functions with different naames.
fn id_i32(x: i32) -> i32 { x } fn id_f32(x: f32) -> f32 { x } fn id_str(x: &str) -> &str { x } fn main() { let int_id = id_i32(10); let f32_id = id_f32(1.1_f32); let string_id = id_str("hello"); println!("{}", int_id); println!("{}", f32_id); println!("{}", string_id); }