0
0
Rustprogramming~10 mins

Basic match usage in Rust - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A1
B4
C0
D5
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.
2fill in blank
medium

Complete 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'
A'b'
B'd'
C'e'
D'f'
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using double quotes instead of single quotes for characters.
Choosing a character that does not match the variable.
3fill in blank
hard

Fix 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'
A15
Bten
C"10"
D10
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using a string "10" instead of the integer 10.
Using a variable name that is not defined.
4fill in blank
hard

Fill 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'
A%
B==
C_
D!=
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using == instead of % for modulus.
Not using _ as a wildcard in the second match arm.
5fill in blank
hard

Fill 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'
Ax
By
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using different variable names in the tuple and the if guard.
Not using variables to destructure the tuple.