Generics
Rust support generics, which lets you abstract algorithms or data structures (such as sorting or a binary tree) over the types used or stored.
Generic Data Types
You can use generics to abstract over the concrete field type:
#[derive(Debug)]
struct Point<T> {
x: T,
y: T,
}
fn main() {
let integer = Point { x: 5, y: 10 };
let float = Point { x: 1.0, y: 4.0 };
println!("{integer:?} and {float:?}");
}
Point { x: 5, y: 10 } and Point { x: 1.0, y: 4.0 }
Notes
- Try declaring a new variable let p = Point { x: 5, y: 10.0 };.
- Fix the code to allow points that have elements of different types.
Generic Methods
You can declare a generic type on your impl block:
#[derive(Debug)]
struct Point<T>(T, T);
impl<T> Point<T> {
fn x(&self) -> &T {
&self.0 // + 10
}
// fn set_x(&mut self, x: T)
}
fn main() {
let p = Point(5, 10);
println!("p.x = {}", p.x());
}
p.x = 5
Notes Q: Why T is specified twice in impl<T> Point<T> {}? Isn’t that redundant?
- This is because it is a generic implementation section for generic type. They are independently generic.
- It means these methods are defined for any T.
- It is possible to write impl Point<u32> { .. }.
- Point is still generic and you can use Point<f64>, but methods in this block will only be available for Point<u32>.
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.