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";
    }
}
Aero 文档 · 基于 Aero 1.2.0 文档整理 · MIT
SereinTeam