1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-04-02 01:36:34 +02:00
comprehensive-rust/src/generics/monomorphization.md
2022-12-21 16:38:28 +01:00

547 B

Monomorphization

Generic code is turned into non-generic code based on the call sites:

fn main() {
    let integer = Some(5);
    let float = Some(5.0);
}

behaves as if you wrote

enum Option_i32 {
    Some(i32),
    None,
}

enum Option_f64 {
    Some(f64),
    None,
}

fn main() {
    let integer = Option_i32::Some(5);
    let float = Option_f64::Some(5.0);
}

This is a zero-cost abstraction: you get exactly the same result as if you had hand-coded the data structures without the abstraction.