Learn Aero / Generics
Generics
Generic Functions
fn id<T>(x: T) -> T {
return x;
}
let a = id(42);
let b = id("hi");Generic struct / enum
struct Pair<A, B> {
first: A,
second: B,
}
enum Wrapper<T> {
Val(T),
Empty,
}Compile-Time Monomorphization
Generics are monomorphized at compile time (one copy of code is generated per concrete type). Therefore:
Generic functions cannot be
#[export], because the exported symbol must be monomorphized. Exported functions need to use concrete types.Generic Traits
trait Describe {
fn describe() -> str;
}
impl Describe for Point {
fn describe() -> str {
return "a point";
}
}