Review an event classifier

from Pattern matching
Rust 1.98 advanced 6 min 5 issues to find

Review this generated event classifier against the stated task.

Borrow the event; return a static label without allocation for a selected read, another read, an accepted write, or a delete; accept writes through 1,024 bytes and return an error above that limit.

Rust
enum Event {
    Read { id: u32 },
    Write { bytes: usize },
    Delete { id: u32 },
}

fn classify(event: &Event, selected_id: u32) -> Result<String, String> {
    match event {
        Event::Read { id: selected_id } => Ok(format!("selected {selected_id}")),
        Event::Read { .. } => Ok(String::from("other read")),
        Event::Write { bytes } if *bytes < 1_024 => Ok(String::from("accepted write")),
        Event::Write { .. } => unreachable!(),
        _ => Ok(String::from("ignored")),
    }
}

fn main() {
    let event = Event::Read { id: 7 };
    println!("{:?}", classify(&event, 9));
}

generated code is illustrative, not from any one model

Open in playground
Report an error