Pattern matching
Pattern matching in Rust is a powerful feature that allows you to match the structure of complex data types. It is similar to the switch
statement in other languages, but it is more powerful and flexible.
Using the "match" keyword
The match
keyword is followed by an expression and a set of arms. Each arm consists of a pattern and an expression to be executed if the pattern matches the expression. The match
keyword is used to match the value of an expression against a set of patterns and execute the corresponding expression.
Here is an example of using the match
keyword to match the value of an expression against a set of patterns:
In this example, the match
keyword is used to match the value of the variable x
against a set of patterns. If the value of x
matches one of the patterns, the corresponding expression is executed. If the value of x
does not match any of the patterns, the _
pattern is used to match any value, and the corresponding expression is executed.
Arms
Arms are the individual clauses in a match
expression. Each arm consists of a few parts: pattern, expression, a Guard (optional).
- Pattern: The pattern is used to match the value of the expression. If the pattern matches the value of the expression, the corresponding expression is executed.
- Expression: The expression will be returned as the result of the
match
expression. - Guard (optional): The guard is an additional condition that must be satisfied for the arm to match.
Guards in a match expression
A guard is simply another condition that must be satisfied for the arm to match. Here is an example of using a guard in a match
expression:
In the example above, we are using a guard to match any value greater than 3
. If the value of x
is greater than 3
, the guard will match, and the corresponding expression will be executed.
Guards in a match expression
Using the "if let" keyword
The if let
keyword is used to perform pattern matching in Rust. The if let
keyword is followed by a pattern and an expression to be executed if the pattern matches the expression. The if let
keyword is used to match the value of an expression against a pattern and execute the corresponding expression.
For example, let's say we have an enum
type called State
, which represents the state of a system, it can be either Idle
, Running
, or Waiting
. We can use the if let
keyword to match the value of the State
enum against a pattern and execute the corresponding expression:
In the example above, we are checking only for the Idle
state, and based on the state (whether it is Idle
or not), we are printing a message.
If let
You can also use the match
keyword to match the value of the State
enum just like we did in the previous example, but if you're using match
you have to match all the possible states, otherwise, you will get a compile-time error.
When it comes to matching patterns in Rust, you have two options, using
match
orif let
. If you want to match all the possible patterns, you should usematch
, but if you want to execute some code based on only one pattern, you should useif let
for simplicity.
Conclusion
In this module, we've learned about control flow in Rust, we started with learning about if
expressions, then we learned about loops
, and finally, we learned about pattern matching
using the match
and if let
keywords. Control flow is an essential part of any programming language, and Rust provides powerful control flow features that make it easy to write complex programs.
Now that you've learned the fundamentals of the Rust programming language, you're ready to build your first Rust program, in the next module, we're going to build a "Guessing Game" in Rust. Let's get started!