15
Patterns
Patterns identify the shape of a value. With pattern syntax, you can apply the pattern to a value to determine if there is a match. There are many benefits to patterns. Well-placed patterns can make code more concise and transparent. In addition, you can create unique solutions with patterns. Patterns are ubiquitous in Rust and just another feature, like lifetimes, that gives Rust source code a distinctive appearance.
The primary use cases for patterns is pattern matching and control flow. For pattern matching, a value is matched against a pattern. If there is a match, the value can be destructured into its components, if any. For control flow, there is a transfer of control when a match occurs. The match, while, if, and other expressions, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access