rust switch
时间: 2023-08-24 12:06:13 浏览: 51
As an AI language model, I'm assuming you're asking about the Rust programming language's `match` statement, which is similar to a switch statement in other programming languages.
The `match` statement in Rust is used to match a value against a set of patterns and execute corresponding code based on the matching pattern. Here is an example of a `match` statement in Rust:
```
let number = 5;
match number {
1 => println!("One"),
2 => println!("Two"),
3 => println!("Three"),
4 => println!("Four"),
5 => println!("Five"),
_ => println!("Other")
}
```
In this example, the `match` statement is matching `number` against a set of patterns. If `number` is equal to 1, the code inside the first pattern will be executed, and so on. If `number` doesn't match any of the patterns, the `_` pattern will match, and the code inside it will be executed.
The `match` statement in Rust is powerful and can be used in many different ways, including matching against enums, structs, and even ranges.