Write a match over a nested event enum with guards, @ and or-patterns
Write describe, which turns an input event into a short label. Decide every case in one match on event with nested patterns.
Requirements:
- a digit key
'0'..='9'givesdigit 7(the digit itself);EnterorEscgivescontrol; any other character key giveskey x; - a click at exactly
(0, 0)givescorner clickwhatever the button; a click withbtn2 anywhere else givesright click; any other click givesclick at (5, 9); - a resize to equal sides gives
square 300; any other resize givesresize 640x480; - no
if/elsechains and no innermatch.
enum Key {
Char(char),
Enter,
Esc,
}
enum Event {
Key(Key),
Click { x: i32, y: i32, btn: u8 },
Resize(u32, u32),
}
fn describe(event: &Event) -> String {
// your code here
}Write the implementation.
Do not add extra use lines unless the task says so explicitly.
One match with nested patterns: Key(Char(d @ '0'..='9')) tests and binds the digit, Key(Enter | Esc) joins two keys, Click { x: 0, y: 0, .. } checks fields and skips the rest, and Resize(w, h) if w == h adds a guard. General arms go last.
- Writing the
@binding after the range: the name comes first, as ind @ '0'..='9' - Expecting guards to count toward exhaustiveness and dropping the plain fallback arm
- Putting a general arm such as
Click { x, y, .. }above the specific ones, which makes them unreachable
- Why must both sides of an or-pattern bind the same names with the same types?
- Why do
d,xandybecome references when thematchis on a&Event?
Each arm describes the whole shape of the event — from the Event variant down to the nested Key or the click fields — so one match with no inner checks is enough:
enum Key {
Char(char),
Enter,
Esc,
}
enum Event {
Key(Key),
Click { x: i32, y: i32, btn: u8 },
Resize(u32, u32),
}
fn describe(event: &Event) -> String {
match event {
Event::Key(Key::Char(d @ '0'..='9')) => format!("digit {d}"),
Event::Key(Key::Enter | Key::Esc) => String::from("control"),
Event::Key(Key::Char(c)) => format!("key {c}"),
Event::Click { x: 0, y: 0, .. } => String::from("corner click"),
Event::Click { btn: 2, .. } => String::from("right click"),
Event::Click { x, y, .. } => format!("click at ({x}, {y})"),
Event::Resize(w, h) if w == h => format!("square {w}"),
Event::Resize(w, h) => format!("resize {w}x{h}"),
}
}
fn main() {
let events = [
Event::Key(Key::Char('7')),
Event::Key(Key::Enter),
Event::Key(Key::Esc),
Event::Key(Key::Char('x')),
Event::Click { x: 0, y: 0, btn: 1 },
Event::Click { x: 5, y: 9, btn: 2 },
Event::Click { x: 5, y: 9, btn: 1 },
Event::Resize(300, 300),
Event::Resize(640, 480),
];
for e in &events {
println!("{}", describe(e)); // digit 7, control, control, key x, corner click, ...
}
}d @ '0'..='9'checks that the character is a digit and puts it intodat the same time. The name sits to the left of@, the pattern to the right.Key::Enter | Key::Escjoins two variants right insideEvent::Key(…):|works at any depth of nesting.Event::Click { x: 0, y: 0, .. }compares two fields with literals, and..skipsbtn. This arm sits above{ btn: 2, .. }, so a right click at(0, 0)givescorner clicktoo, as the requirements say.- The compiler ignores the guard
if w == hwhen it checks exhaustiveness, so the last arm,Event::Resize(w, h), has no guard. - Order matters: the general arms (
Key::Char(c),Click { x, y, .. },Resize(w, h)) come after the specific ones, or the specific ones would be unreachable.
The match is on a &Event, so every binding (d, c, x, y, w, h) is a reference; format! and == work with them without an explicit dereference.