Pattern matching helps Rust check different cases clearly and safely. It makes code easier to read and avoids mistakes.
Why pattern matching is needed in Rust
enum Option<T> { Some(T), None, } fn check_option(value: Option<i32>) { match value { Some(number) => println!("Got number: {}", number), None => println!("No value found"), } }
The match keyword checks the value against patterns.
Each pattern is followed by code to run if it matches.
number has a value or not.let number = Some(5); match number { Some(value) => println!("Value is {}", value), None => println!("No value"), }
let number: Option<i32> = None;
match number {
Some(value) => println!("Value is {}", value),
None => println!("No value"),
}enum Color { Red, Green, Blue, } let color = Color::Green; match color { Color::Red => println!("Red color"), Color::Green => println!("Green color"), Color::Blue => println!("Blue color"), }
This program shows how pattern matching helps handle both cases of an option: when it has a number and when it does not.
enum Option<T> { Some(T), None, } fn describe_option(value: Option<i32>) { match value { Some(number) => println!("We have the number: {}", number), None => println!("No number found"), } } fn main() { let value_with_number = Option::Some(10); let value_none: Option<i32> = Option::None; println!("Before matching:"); println!("value_with_number = Some(10)"); println!("value_none = None"); println!("\nAfter matching:"); describe_option(value_with_number); describe_option(value_none); }
Pattern matching is very fast because Rust checks patterns in order and stops at the first match.
It uses little extra memory since it just compares values.
A common mistake is forgetting to cover all possible cases, which Rust helps prevent by giving errors.
Use pattern matching instead of many if-else statements for clearer and safer code.
Pattern matching helps handle different data cases clearly and safely.
It makes code easier to read and maintain.
Rust's compiler checks that all cases are covered, preventing bugs.
Practice
Solution
Step 1: Understand pattern matching purpose
Pattern matching lets you check different forms of data and handle each case clearly.Step 2: Compare with other options
The other options describe unrelated or incorrect benefits.Final Answer:
It helps handle different data cases clearly and safely. -> Option AQuick Check:
Pattern matching = clear, safe data handling [OK]
- Thinking pattern matching speeds up code execution
- Believing it fixes syntax errors automatically
- Confusing pattern matching with function replacement
Solution
Step 1: Identify Rust pattern matching syntax
Rust uses the keywordmatchfollowed by the value and braces.Step 2: Eliminate other language syntax
The other options use syntax from other languages or incorrect forms.Final Answer:
match value { ... } -> Option CQuick Check:
Rust pattern match starts with 'match' [OK]
- Using 'switch' instead of 'match'
- Trying to use 'case' like in other languages
- Confusing 'if' statement with pattern matching
let number = Some(5);
match number {
Some(x) if x > 3 => println!("Greater than 3"),
Some(_) => println!("3 or less"),
None => println!("No value"),
}Solution
Step 1: Analyze the matched value
The variablenumberisSome(5), so it matchesSome(x)with x=5.Step 2: Check the guard condition
The first arm has a conditionif x > 3, which is true for 5, so it prints "Greater than 3".Final Answer:
Greater than 3 -> Option BQuick Check:
Some(5) with x>3 prints 'Greater than 3' [OK]
- Ignoring the if guard and picking second arm
- Thinking None matches Some(5)
- Assuming compilation error due to guard
let value = 10;
match value {
1 => println!("One"),
2 => println!("Two"),
}Solution
Step 1: Check match arms coverage
The match only covers 1 and 2, but value is 10, so no arm matches.Step 2: Understand Rust match exhaustiveness
Rust requires all possible cases covered or a catch-all arm like_.Final Answer:
Missing a catch-all pattern to cover other values -> Option AQuick Check:
Match must cover all cases or use catch-all [OK]
- Forgetting match must be exhaustive
- Thinking println! usage is wrong
- Assuming variable must be mutable
Solution
Step 1: Check correct pattern matching syntax
match opt { Some(n) => println!("Number: {}", n), None => println!("No number"), } matches Some(n) and None correctly and prints accordingly.Step 2: Evaluate other options
The option with a guard conditionif n > 0incorrectly excludes non-positive numbers. Theif letapproach works but uses destructuring instead of a fullmatch. The option withSome =>lacks variable binding and would print theOptionrather than the number.Final Answer:
match opt { Some(n) => println!("Number: {}", n), None => println!("No number"), } -> Option DQuick Check:
Correct pattern match covers Some and None [OK]
- Using if let instead of match (not pattern matching)
- Missing variable in Some pattern
- Adding unnecessary conditions excluding valid values
Ready to prove it?
Pass the test and this pattern moves unknown
