dev-resources.site
for different kinds of informations.
Rust Match Pattern Cheatsheet
Published at
12/18/2024
Categories
rust
programming
Author
Confidence Okoghenun
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!
Articles
12 articles in total
Rust Match Pattern Cheatsheet
currently reading
Here's How Building a HTTP Stream Processor Went
read article
What Is WebAuthN: A Developer's Guide To WebAuthN
read article
How To Clone An RFID/NFC Access Card
read article
How To Self-Host Cloudflare
read article
How To Use The Web Share Target API
read article
Relocating? Ultimate Guide To Find Your Next Apartment
read article
How The Rust Module System Works
read article
How To Use The Web Bluetooth API
read article
Ultimate Guide To Rust Lifetimes For Newbies
read article
Rust Memory Management: Borrowing, References, And The Borrow Checker
read article
Beginner Guide To Memory Management In Rust: Ownership
read article
Featured ones: