Using Match Statements in Rust

The match statement in Rust is a powerful control flow construct that allows you to compare a value against a series of patterns and execute code based on which pattern matches. It is similar to switch statements in other languages but is more expressive and can handle complex patterns.

1. Basic Syntax of Match Statements

The basic syntax of a match statement is as follows:


match value {
pattern1 => expression1,
pattern2 => expression2,
_ => default_expression, // Optional catch-all pattern
}

Here, value is the expression being matched, and each pattern is a possible match for that value. The => operator separates the pattern from the expression that should be executed if the pattern matches. The underscore _ is a catch-all pattern that matches any value not covered by the previous patterns.

2. Example of a Match Statement

Let's look at a simple example that matches an integer value:


fn main() {
let number = 3;

match number {
1 => println!("One"),
2 => println!("Two"),
3 => println!("Three"),
_ => println!("Not one, two, or three"), // Catch-all pattern
}
}

Explanation of the Example

  • We declare a variable number and assign it the value 3.
  • The match statement checks the value of number against the specified patterns.
  • When it finds a match (in this case, 3), it executes the corresponding expression, printing "Three" to the console.
  • If number had a value not covered by the specified patterns, the catch-all pattern would execute, printing "Not one, two, or three."

3. Matching with Multiple Patterns

You can also match multiple patterns in a single arm by separating them with a vertical bar |.

Example of Multiple Patterns


fn main() {
let number = 2;

match number {
1 | 2 => println!("One or Two"), // Matches both 1 and 2
3 => println!("Three"),
_ => println!("Not one, two, or three"),
}
}

Explanation of the Example

  • In this example, the match arm 1 | 2 will execute if number is either 1 or 2, printing "One or Two."
  • This feature allows for concise handling of multiple cases that should execute the same code.

4. Matching with Ranges

Rust's match statement can also match ranges of values using the .. syntax.

Example of Matching Ranges


fn main() {
let number = 7;

match number {
1..=5 => println!("Between one and five"), // Inclusive range
6..=10 => println!("Between six and ten"),
_ => println!("Out of range"),
}
}

Explanation of the Example

  • In this example, the match arms 1..=5 and 6..=10 match inclusive ranges of values.
  • If number is between 1 and 5, it prints "Between one and five." If it is between 6 and 10, it prints "Between six and ten."

5. Conclusion

The match statement in Rust is a versatile and powerful tool for control flow, allowing for pattern matching that can handle a variety of cases, including simple values, multiple patterns, and ranges. Its expressive syntax makes it a preferred choice for many Rust developers when dealing with conditional logic. Understanding how to effectively use match statements can greatly enhance the readability and maintainability of your Rust code.