rustUsing enum match in Rust
Enum match in Rust is a powerful tool for pattern matching. It allows you to match on the value of an enum and execute code based on the value. For example, the following ## Code example uses enum match to match on the value of a Color enum and print a message based on the value:
enum Color {
Red,
Blue,
Green,
}
fn main() {
let color = Color::Red;
match color {
Color::Red => println!("The color is Red"),
Color::Blue => println!("The color is Blue"),
Color::Green => println!("The color is Green"),
}
}
The output of this ## Code example is:
The color is Red
The ## Code example uses the enum match syntax to match on the value of the Color enum. If the value of the enum is Red, the code block associated with the Color::Red match arm is executed. If the value of the enum is Blue, the code block associated with the Color::Blue match arm is executed, and so on.
Helpful links
Related
- How to create enum from string in Rust
- How to use enum as hashmap key in Rust
- How to create enum from number in Rust
- How to use fmt for enum in Rust
- How to declare enum in Rust
- How to compare enum in Rust
- How to serialize enum in Rust
- How to loop through enum in Rust
- How to cast enum in Rust
- Get certain enum value in Rust
More of Rust
- Hashshet example in Rust
- How to convert Rust bytes to hex?
- How to convert a Rust HashMap to a JSON string?
- How to convert a Rust HashMap to JSON?
- How to get the last element of a Rust slice?
- How to use non-capturing groups in Rust regex?
- How to use groups in a Rust regex?
- How to match the end of a line in a Rust regex?
- How to escape dots with regex in Rust?
- How to use regex to match a group in Rust?
See more codes...