Complete the code to print a message based on the number using match.
let number = 1; match number { [1] => println!("One"), 2 => println!("Two"), _ => println!("Other"), }
The match arm for 1 matches the number 1 and prints "One".
Complete the code to match a character and print its type.
let c = 'a'; match c { [1] => println!("Vowel"), 'b' | 'c' => println!("Consonant"), _ => println!("Other"), }
The match arm for 'a' matches the vowel character and prints "Vowel".
Fix the error in the match expression to correctly handle the boolean value.
let flag = true;
match flag {
[1] => println!("True"),
false => println!("False"),
}Boolean values in Rust are lowercase true and false.
Fill both blanks to match a tuple and print the correct message.
let pair = (0, -2); match pair { ([1], [2]) => println!("Matched zero and negative number"), _ => println!("No match"), }
The tuple (0, -2) matches the pattern with first element 0 and second element -2.
Fill all three blanks to match an enum and print the correct message.
enum Direction {
Up,
Down,
Left,
Right,
}
let dir = Direction::Left;
match dir {
Direction::[1] => println!("Going up"),
Direction::[2] => println!("Going down"),
Direction::[3] => println!("Going left"),
_ => println!("Other direction"),
}The match arms correspond to the enum variants Up, Down, and Left.
