Remembering all the various patterns that can be used in a match expression can be quite tricky, so here’s a free comprehensive list you can reference anytime 😄.
| Pattern | Example |
|---|---|
| Wildcard | _ |
| Literals | 300 ”john” |
| Range | 0 ..= 10 ’a’ ..= ‘z’ |
| Variable | name mut name (Moves value into variable name) |
| ref Variable | ref name ref mut name |
| Reference | &value &(a, b) (Matches only reference values) |
| Tuple | (x, y, z) |
| Array | [r, g, b] |
| Slice | [a, b] [a, _, c] [a, .., z] |
| Enum | Some(value) None Day::Monday |
| Struct | Axis(x, y) Person {name, age} Profile {id, name, …} Shape {length: 10, active: true} |
| @ Binding | val @ 0 ..= 20 ref square @ Shape::Square {..} |
| Multiple patterns | ‘a’ \ ‘b’ |
| Guard expression | x if x*x <= 10 |
Love to connect? Find me online on LinkedIn or Twitter. Bye!
Top comments (0)