Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The match arm must match the value 3 exactly, so use 3.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
Using _ ignores the first element, so y is the second element printed.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Matching the value 7 directly instead of the reference.
Using
*7 which is invalid syntax in patterns.✗ Incorrect
The variable num is a reference, so the pattern must match &7.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect variant names like
RgbColor.Mixing up the order of variants.
✗ Incorrect
The enum variants are Rgb and Cmyk. Use these exact names to match.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping field names or binding the wrong variable.
Using incorrect syntax for struct pattern matching.
✗ Incorrect
Match the field x to 0, bind y to a variable y to print it.