Docs rewrite
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
---
|
||||
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 `'`.
|
||||
Reference in New Issue
Block a user