DEV Community

Cover image for Rust Match Pattern Cheatsheet
Confidence Okoghenun
Confidence Okoghenun

Posted on Originally published at confidence.sh

Rust Match Pattern Cheatsheet

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)