Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print a message based on the number using match.
Rust
let number = 1; match number { [1] => println!("One"), 2 => println!("Two"), _ => println!("Other"), }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using a value that does not match the variable.
Forgetting to cover all cases with a wildcard.
โ Incorrect
The match arm for 1 matches the number 1 and prints "One".
2fill in blank
mediumComplete the code to match a character and print its type.
Rust
let c = 'a'; match c { [1] => println!("Vowel"), 'b' | 'c' => println!("Consonant"), _ => println!("Other"), }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using double quotes instead of single quotes for characters.
Matching a character not covered in the arms.
โ Incorrect
The match arm for 'a' matches the vowel character and prints "Vowel".
3fill in blank
hardFix the error in the match expression to correctly handle the boolean value.
Rust
let flag = true;
match flag {
[1] => println!("True"),
false => println!("False"),
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using uppercase or quoted booleans.
Using a string instead of a boolean.
โ Incorrect
Boolean values in Rust are lowercase true and false.
4fill in blank
hardFill both blanks to match a tuple and print the correct message.
Rust
let pair = (0, -2); match pair { ([1], [2]) => println!("Matched zero and negative number"), _ => println!("No match"), }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Swapping the tuple elements.
Using positive numbers instead of negative.
โ Incorrect
The tuple (0, -2) matches the pattern with first element 0 and second element -2.
5fill in blank
hardFill all three blanks to match an enum and print the correct message.
Rust
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"),
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using lowercase variant names.
Mixing up the order of variants.
โ Incorrect
The match arms correspond to the enum variants Up, Down, and Left.