0
0
Rustprogramming~10 mins

Match overview 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 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'
A0
B3
C4
D1
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using a value that does not match the variable.
Forgetting to cover all cases with a wildcard.
2fill in blank
medium

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

Fix 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'
Atrue
BTrue
C"true"
DTRUE
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using uppercase or quoted booleans.
Using a string instead of a boolean.
4fill in blank
hard

Fill 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'
A0
B1
C-2
D2
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Swapping the tuple elements.
Using positive numbers instead of negative.
5fill in blank
hard

Fill 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'
AUp
BDown
CLeft
DRight
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using lowercase variant names.
Mixing up the order of variants.