LogoMist

Generics

Generic functions, structs, enums, trait bounds, and lifetimes.

// Generic function
T id<T>(T x)
{
    x
}

// Generic struct
struct Pair<A, B>
{
    A first,
    B second,
}

// Generic enum
enum Result<T, E>
{
    Ok(T);
    Err(E);
}

// Generic with trait bounds
T max<T : Ord>(T a, T b)
{
    if a > b { a } else { b }
}

// Lifetime generics
void process<'a>(&'a str& data)
{
    // ...
}

Generic syntax uses < > delimiters. Lifetimes are prefixed with '.