0
0
Rustprogramming~10 mins

Match expression deep dive 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 number 3 and print a message.

Rust
let number = 3;
match number {
    [1] => println!("Three!"),
    _ => println!("Not three"),
}
Drag options to blanks, or click blank then click option'
Anumber
B3
C"3"
D4
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around 3 makes it a string, which won't match the integer.
Using a variable name instead of the literal value.
2fill in blank
medium

Complete the code to match a tuple and print the second element.

Rust
let pair = (5, 10);
match pair {
    ([1], y) => println!("Second is {}", y),
    _ => println!("No match"),
}
Drag options to blanks, or click blank then click option'
A_
Bx
C5
Dy
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable for the first element but printing the wrong variable.
Trying to match the exact value 5 instead of ignoring it.
3fill in blank
hard

Fix the error in the match arm to correctly match a reference to a number.

Rust
let num = &7;
match num {
    [1] => println!("Seven!"),
    _ => println!("Not seven"),
}
Drag options to blanks, or click blank then click option'
Aref 7
B7
C*7
D&7
Attempts:
3 left
💡 Hint
Common Mistakes
Matching the value 7 directly instead of the reference.
Using *7 which is invalid syntax in patterns.
4fill in blank
hard

Fill both blanks to match an enum variant and extract its value.

Rust
enum Color {
    Rgb(u8, u8, u8),
    Cmyk(u8, u8, u8, u8),
}

let color = Color::Rgb(10, 20, 30);
match color {
    Color::[1](r, g, b) => println!("Red: {}, Green: {}, Blue: {}", r, g, b),
    Color::[2](_, _, _, _) => println!("CMYK color"),
}
Drag options to blanks, or click blank then click option'
ARgb
BCmyk
CRgbColor
DCmykColor
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect variant names like RgbColor.
Mixing up the order of variants.
5fill in blank
hard

Fill all three blanks to match a struct and extract fields with pattern matching.

Rust
struct Point {
    x: i32,
    y: i32,
}

let p = Point { x: 0, y: 7 };
match p {
    Point { [1]: 0, [2]: [3] } => println!("On the y axis at {}", y),
    _ => println!("Not on the y axis"),
}
Drag options to blanks, or click blank then click option'
Ax
By
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping field names or binding the wrong variable.
Using incorrect syntax for struct pattern matching.