40 lines
532 B
Plaintext
40 lines
532 B
Plaintext
---
|
|
title: Generics
|
|
description: Generic functions, structs, enums, trait bounds, and lifetimes.
|
|
icon: Braces
|
|
---
|
|
|
|
```mist
|
|
// 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 `'`.
|