Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to match the value and print the correct message.
Rust
let number = 3; match number { [1] => println!("One"), 2 => println!("Two"), 3 => println!("Three"), _ => println!("Other"), }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using a number that does not match the arm, causing the wrong message to print.
Leaving the blank empty or with invalid syntax.
โ Incorrect
The match arm for 1 should be 1 to correctly match the value and print "One".
2fill in blank
mediumComplete the code to match a character and print the correct message.
Rust
let letter = 'b'; match letter { 'a' => println!("A"), [1] => println!("B"), 'c' => println!("C"), _ => 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.
Choosing a character that does not match the variable.
โ Incorrect
The match arm should be 'b' to match the variable letter and print "B".
3fill in blank
hardFix the error in the match expression by completing the blank.
Rust
let value = 10; match value { 5 => println!("Five"), [1] => println!("Ten"), _ => println!("Other"), }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using a string "10" instead of the integer 10.
Using a variable name that is not defined.
โ Incorrect
The match arm must be 10 (an integer) to match the value variable correctly.
4fill in blank
hardFill both blanks to create a match that prints if a number is even or odd.
Rust
let num = 4; match num [1] 2 { 0 => println!("Even"), [2] => println!("Odd"), }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using == instead of % for modulus.
Not using _ as a wildcard in the second match arm.
โ Incorrect
Use the modulus operator % to get the remainder and _ to match all other cases (odd numbers).
5fill in blank
hardFill all three blanks to match a tuple and print the correct message.
Rust
let pair = (2, 3); match pair { ([1], [2]) if [3] == 2 => println!("First is two"), _ => println!("Other"), }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using different variable names in the tuple and the if guard.
Not using variables to destructure the tuple.
โ Incorrect
Use variables x and y to destructure the tuple, then check if x equals 2.