Rust Pattern Matching Quiz
40 comprehensive questions on Rust's pattern matching system, covering match expressions, destructuring, guards, wildcards, and enum matching — with 11 code examples demonstrating practical pattern matching techniques.
Question 1
What is pattern matching in Rust and why is it important?
Question 2
What is the basic syntax of a match expression in Rust?
Question 3
What does it mean for a match expression to be exhaustive?
Question 4
What will this match expression return?
fn main() {
let number = 3;
let result = match number {
1 => "one",
2 => "two",
3 => "three",
_ => "other",
};
println!("Result: {}", result);
}Question 5
What is destructuring in pattern matching?
Question 6
How do you destructure a tuple in a match expression?
Question 7
What will this tuple destructuring code output?
fn main() {
let triple = (1, 2, 3);
match triple {
(0, y, z) => println!("First is 0, y = {}, z = {}", y, z),
(1, y, z) => println!("First is 1, y = {}, z = {}", y, z),
_ => println!("Something else"),
}
}Question 8
What are guards in pattern matching and how do you use them?
Question 9
How do you use a guard in a match expression?
Question 10
What will this code with a guard output?
fn main() {
let num = 4;
match num {
x if x % 2 == 0 => println!("Even number: {}", x),
x if x % 2 == 1 => println!("Odd number: {}", x),
_ => println!("Something else"),
}
}Question 11
What is the underscore (_) wildcard pattern used for?
Question 12
When should you use the underscore pattern in match expressions?
Question 13
What does the double dot (..) syntax do in patterns?
Question 14
How do you destructure a struct in a match pattern?
Question 15
What will this struct destructuring code do?
struct Point {
x: i32,
y: i32,
}
fn main() {
let p = Point { x: 10, y: 20 };
match p {
Point { x: 0, y: 0 } => println!("Origin"),
Point { x, y } => println!("Point at ({}, {})", x, y),
}
}Question 16
What is enum matching and why is it particularly useful in Rust?
Question 17
How do you match on enum variants in a match expression?
Question 18
What will this enum matching code output?
enum Message {
Quit,
Move { x: i32, y: i32 },
Write(String),
ChangeColor(i32, i32, i32),
}
fn main() {
let msg = Message::Write(String::from("hello"));
match msg {
Message::Quit => println!("Quit"),
Message::Move { x, y } => println!("Move to ({}, {})", x, y),
Message::Write(text) => println!("Write: {}", text),
Message::ChangeColor(r, g, b) => println!("Color: ({}, {}, {})", r, g, b),
}
}Question 19
What happens if you don't handle all enum variants in a match expression?
Question 20
How do you handle multiple enum variants with the same behavior?
Question 21
What is match ergonomics in Rust?
Question 22
How do you match on references in patterns?
Question 23
What will this reference matching code do?
fn main() {
let value = Some(42);
match &value {
Some(v) => println!("Got {}", v),
None => println!("Nothing"),
}
// value is still accessible here
println!("Value is still: {:?}", value);
}Question 24
What are match arms and how do they work?
Question 25
How do you use pattern matching with if let for simpler cases?
Question 26
What is the difference between match and if let?
Question 27
How do you match on ranges of values?
Question 28
What will this range matching code output?
fn main() {
let number = 15;
match number {
0..=10 => println!("Low"),
11..=20 => println!("Medium"),
_ => println!("High"),
}
}Question 29
What is binding in pattern matching?
Question 30
How do you use @ bindings in patterns?
Question 31
What will this @ binding code demonstrate?
fn main() {
let msg = Some(42);
match msg {
value @ Some(42) => println!("Got the answer: {:?}", value),
Some(other) => println!("Got something else: {}", other),
None => println!("Nothing"),
}
}Question 32
What is the ref keyword used for in patterns?
Question 33
When should you use ref in match patterns?
Question 34
What is the mut keyword used for in patterns?
Question 35
How do you handle complex nested patterns in match expressions?
Question 36
What will this nested pattern matching do?
enum Shape {
Circle(f64),
Rectangle(f64, f64),
}
fn main() {
let shape = Shape::Rectangle(10.0, 20.0);
match shape {
Shape::Circle(radius) => println!("Circle with radius {}", radius),
Shape::Rectangle(width, height) => println!("Rectangle {}x{}", width, height),
}
}Question 37
What is the purpose of match expressions returning values?
Question 38
How do you make match expressions more concise when many arms have the same result?
Question 39
In a scenario where you're implementing a calculator that handles different operations, how would you structure the pattern matching?
Question 40
What advanced pattern matching technique would you use when you need to match a pattern but also keep a reference to the original value for later use?
