Rust Match
match compares a value to patterns and the arms must cover every case. Use | for several patterns, ranges, guards with if, and _ as a catch all.
Example
let n = 7;
let s = match n { 1 => "one", 2 | 3 => "few", 4..=9 => "several", _ => "many" };
println!("{}", s);
let msg = match n { x if x % 2 == 0 => "even", _ => "odd" };
println!("{}", msg);Result
several odd
Exercise
Catch all pattern.
match x { 1 => 0, => 1 }
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.