rust / beginner
Snippet
Pattern Matching with the match Operator
The match operator allows you to compare a value against a series of patterns and execute code based on which pattern matches.
snippet.rs
1
2
3
4
5
6
7
8
let coin_value = 5;match coin_value {1 => println!("Penny"),5 => println!("Nickel"),10 => println!("Dime"),_ => println!("Unknown coin"),}
Breakdown
1
match coin_value { ... }
Starts the match expression to check the value of coin_value.
2
1 => ...
A match arm: if the value is 1, execute the following expression.
3
_ => ...
The catch-all pattern that matches any value not previously listed.