---
title: "Explain the concept of pattern matching in Rust and provide an example."  
description: "Explain the concept of pattern matching in Rust and provide an example."  
author: "Steilla Mitchel"  
published: 2023-07-10  
updated: 2023-07-11  
canonical: https://www.mindstick.com/forum/159013/explain-the-concept-of-pattern-matching-in-rust-and-provide-an-example  
category: "rust"  
tags: ["programming language", "rust"]  
reading_time: 2 minutes  

---

# Explain the concept of pattern matching in Rust and provide an example.

[Explain the concept](https://www.mindstick.com/forum/159605/explain-the-concept-of-unique-key-violation-error) of [pattern matching](https://www.mindstick.com/articles/1865/pattern-matching-in-erlang) in [Rust](https://www.mindstick.com/forum/160170/explain-rust-s-ownership-borrowing-and-lifetimes-system) and provide an example.

## Replies

### Reply by Aryan Kumar

Sure. [Pattern](https://www.mindstick.com/forum/2314/what-is-the-mvp-and-mvc-pattern) matching is a feature of Rust that allows you to match values against patterns. Patterns can be used to extract values from expressions, to control the flow of execution, and to deconstruct data structures.

Here is an example of pattern matching in Rust:

Rust

```plaintext
fn match_number(number: i32) {
    match number {
        1 => println!("One"),
        2 => println!("Two"),
        3 => println!("Three"),
        _ => println!("Unknown"),
    }
}

fn main() {
    match_number(1);
    match_number(2);
    match_number(3);
    match_number(4);
}
```

This code will print the following output:

Code snippet

```plaintext
One
Two
Three
Unknown
```

The `match` expression in the `match_number()` function takes a number as input and matches it against a series of patterns. If the number matches one of the patterns, the corresponding code block is executed. If the number does not match any of the patterns, the `_` pattern is matched, which is a catch-all pattern that will match any value.

In this example, the `match` expression matches the number 1 against the pattern `1`, which will cause the code block `println!("One");` to be executed. The number 2 matches the pattern `2`, and the number 3 matches the pattern `3`. The number 4 does not match any of the patterns, so the `_` pattern is matched, which causes the code block `println!("Unknown");` to be executed.

Here are some of the benefits of pattern matching in Rust:

- **Expressiveness:** Pattern matching allows you to express complex logic in a concise way.
- **Safety:** Pattern matching can be used to ensure that values are used in a safe way.
- **Performance:** Pattern matching can be used to improve the performance of code by avoiding unnecessary computations.

Overall, pattern matching is a powerful tool that can help you to write more expressive, safe, and performant code in Rust.


---

Original Source: https://www.mindstick.com/forum/159013/explain-the-concept-of-pattern-matching-in-rust-and-provide-an-example

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
