From 931eb0894e7cd3717f1218f0eb06382b11734c13 Mon Sep 17 00:00:00 2001 From: Jasper Ras Date: Sun, 3 Aug 2025 22:29:47 +0200 Subject: vault backup: 2025-08-03 22:29:47 --- Pattern matching with a match expression.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Pattern matching with a match expression.md (limited to 'Pattern matching with a match expression.md') 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!!"), +} +``` -- cgit v1.2.3