Pattern matching is a programming construct in which we can match a given expression or value against a pattern. This can be used to implement control flow logic in a more expressive and concise way than it is possible with traditional conditional statements. Pattern matching is a common feature in Functional languages such as Haskell, OCaml and Elixir.
In Rust, pattern matching can be used in a variety of ways, from let
and match
expressions for flow control to function parameters as a way to destructure complex data. Like in OCaml, Rust’s match
expressions are exhaustive, meaning they must handle all possible cases for a given matched expression.
When combined with Algebraic data types, pattern matching can become a very powerful and elegant tool to describe control flow and computation in general.