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, ...

Get Programming with Rust now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.