diff options
Diffstat (limited to 'Pattern matching with a match expression.md')
-rw-r--r-- | Pattern matching with a match expression.md | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/Pattern matching with a match expression.md b/Pattern matching with a match expression.md new file mode 100644 index 0000000..affc752 --- /dev/null +++ b/Pattern matching with a match expression.md @@ -0,0 +1,13 @@ +[[Rust]] + +This looks like a regular function call but then followed by a block of code. It is called a match expression. A match expression consists of arms, that are the pattern to match and the code to execute. + +For example if a function returns an enum it is a nice way to write each case, it is somewhat similar to a switch statement. + +```rust +guess.cmp(&secret_number) { + Ordering::Less => println!("Too small!"), + Ordering::Greater => println!("Too big!"), + Ordering::Equal => println!("You win!!"), +} +``` |