1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-04-26 17:23:01 +02:00
comprehensive-rust/src/generics/monomorphization.md

33 lines
547 B
Markdown
Raw Normal View History

2022-12-21 16:36:30 +01:00
# Monomorphization
Generic code is turned into non-generic code based on the call sites:
```rust,editable
fn main() {
let integer = Some(5);
let float = Some(5.0);
}
```
behaves as if you wrote
```rust,editable
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.