Complete the code to match the number 3 and print a message.
let number = 3; match number { [1] => println!("Three!"), _ => println!("Not three"), }
The match arm must match the value 3 exactly, so use 3.
Complete the code to match a tuple and print the second element.
let pair = (5, 10); match pair { ([1], y) => println!("Second is {}", y), _ => println!("No match"), }
Using _ ignores the first element, so y is the second element printed.
Fix the error in the match arm to correctly match a reference to a number.
let num = &7; match num { [1] => println!("Seven!"), _ => println!("Not seven"), }
*7 which is invalid syntax in patterns.The variable num is a reference, so the pattern must match &7.
Fill both blanks to match an enum variant and extract its value.
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"),
}RgbColor.The enum variants are Rgb and Cmyk. Use these exact names to match.
Fill all three blanks to match a struct and extract fields with pattern matching.
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"),
}Match the field x to 0, bind y to a variable y to print it.
