Docs rewrite
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
---
|
||||
title: Patterns
|
||||
description: Pattern matching with literals, tuples, structs, named tuples, wildcards, and mutable bindings.
|
||||
icon: Split
|
||||
---
|
||||
|
||||
```mist
|
||||
// Literal patterns
|
||||
match x {
|
||||
1 => "one",
|
||||
2 => "two",
|
||||
_ => "other",
|
||||
}
|
||||
|
||||
// Tuple patterns
|
||||
let (a, b) = (1, 2);
|
||||
|
||||
// Struct patterns
|
||||
match value {
|
||||
Point { x, y } => x + y,
|
||||
Point { x: 0, y } => y,
|
||||
_ => 0,
|
||||
}
|
||||
|
||||
// Named tuple patterns (newtype)
|
||||
let MyType(value) = my_var;
|
||||
|
||||
// Wildcard / etc
|
||||
let _ = get_side_effect();
|
||||
match x {
|
||||
1 => ...,
|
||||
.. => ..., // rest / etc
|
||||
}
|
||||
|
||||
// Mutable binding in pattern
|
||||
let mut x = 42;
|
||||
match ref_to_option {
|
||||
Some(mut value) => value += 1,
|
||||
None => {},
|
||||
}
|
||||
```
|
||||
Reference in New Issue
Block a user