Rust match Examples (match Some)Use the match keyword to perform pattern matching. Compare match with if, and use Some in match expressions.
Match. With Rust's match, we choose from a selection of expressions, and run the code from the matching expression. Only one path can be taken—a default case is possible.
Some details. We can assign a value to the result value of match—in this way match is like a function. And Options (Some) can be used in expressions.
First example. To begin, we use both the match keyword, and the if-else keywords to implement selection statements in Rust. The result of the 2 approaches here is the same.
Version 1 We use match—if the ID if 5, we print a certain message. In this program, the ID is 10, so we match the default ("_") case.
Version 2 The if-else block here contains the same logic as the match keyword. The code seems to be less elegant, and longer.
Rust program that compares match, if-keyword
fn main() { let id = 10; // Version 1: use match. match id { 5 => println!("Id is 5"), _ => println!("Id is not 5") } // Version 2: use if-else. if id == 5 { println!("Id is 5"); } else { println!("Id is not 5"); } }
Id is not 5 Id is not 5
Assign example. Match can be used like an expression—it can be assigned to a variable. Here we have the string "rust," and we match that string in a match statement.
Result The bool "true" is used, and this value is stored in the memory location of the "compiled" variable.
Rust program that assigns to result of match
fn main() { let language = "rust"; // Assign a boolean based on the result of match. // ... Default is false. let compiled = match language { "rust" => true, "java" => true, _ => false }; println!("COMPILED: {}", compiled); }
Example, Some. In Rust, many functions (like find) return an Option. We can use pattern matching to match the possible options returned.
Tip Find here returns Some(1), which is matched in the match statement, and a message is printed.
find, rfind
Rust program that uses match with Some, Option
fn main() { let letters = "abc"; // Use match on an Option result. // ... Use Some() to match the returned option with its enclosed value. match letters.find("b") { Some(5) => println!("Found at index 5"), Some(1) => println!("Found at index 1"), _ => println!("Not found") } }
Found at index 1
Multiple values. In Rust the match statement can handle groups of values in a single block. We match 2 cases in a single block by using the vertical bar.
Info The values 0 and 10 are grouped together in this example. So the result is the same for values 0 and 10.
Rust program that uses match, multiple constants
fn main() { let value = 10; let result = match value { 5 => "Five", 0 | 10 => "Zero or ten", _ => "Unknown" }; // Print result. println!("VALUE = {}, RESULT = {}", value, result); }
VALUE = 10, RESULT = Zero or ten
Ranges. Inclusive ranges can be used as match conditions. Here we try to match the values 4 through 10 (including 10) in the first match condition.
And The value is 11, so it is matched by the second condition which is from 11 up to and including 20.
Rust program that uses ranges in match
fn main() { let value = 11; // Match with ranges. let result = match value { 4..=10 => "Four through ten", 11..=20 => "Eleven through twenty", _ => "Default" }; println!("{}", result); }
Eleven through twenty
Non-exhaustive error. In Rust a match must handle all possible values of a type. If it does not, we will get a non-exhaustive patterns compile-time error.
Rust program that shows non-exhaustive error
fn main() { let value = 0; // Must add the default case here to compile. match value { 1 => println!("ONE") } }
error[E0004]: non-exhaustive patterns: i32::MIN..=0_i32 and 2_i32..=i32::MAX not covered help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms note: the matched value is of type i32
A summary. Often we use match with Option and Some, but we do not need to. Match statements can replace if-statements, and the resulting code is often clearer.
© 2007-2022 sam allen.
see site info on the changelog.