Complete the code to print a message based on the number using pattern matching.
let number = 1; match number { [1] => println!("One"), 2 => println!("Two"), _ => println!("Other"), }
The pattern 1 matches when number is 1, so the message "One" is printed.
Complete the code to match a character and print its type.
let ch = 'a'; match ch { [1] => println!("Vowel"), 'b' | 'c' => println!("Consonant"), _ => println!("Other"), }
The pattern 'a' matches the character ch when it is 'a', printing "Vowel".
Fix the error in the match pattern to correctly match the tuple.
let pair = (0, -2); match pair { ([1], y) if y < 0 => println!("Negative y"), _ => println!("Other"), }
The pattern (0, y) matches when the first element is 0 and binds the second element to y.
Fill both blanks to create a pattern that matches a Some variant with a positive number.
let value = Some(5); match value { Some([1]) if [2] > 0 => println!("Positive number"), _ => println!("Other"), }
Using x binds the inner value of Some and checks if it is greater than 0.
Fill all three blanks to destructure a struct and print its fields.
struct Point { x: i32, y: i32 }
let p = Point { x: 10, y: 20 };
match p {
Point { [1]: a, [2]: b } if a < b => println!("x < y"),
Point { [3]: _, y } => println!("Other case"),
}The first match arm destructures x and y into a and b. The second arm matches y and ignores x.