Exercise 21 / 24: Incident Queue

// Iterators of Code 2026 — Day 21: Incident Queue
// Challenge: Express the selection without manually tracking the best match.
// Larger numbers mean higher priority. Ties choose the LAST incident in the
// input, not the first. Return None when the queue is empty.

#[derive(Debug, PartialEq)]
struct Incident {
    name: &'static str,
    priority: u8,
}

fn next_incident(incidents: &[Incident]) -> Option<&Incident> {
    let mut best: Option<&Incident> = None;
    for incident in incidents {
        match best {
            Some(current) if current.priority > incident.priority => {}
            _ => best = Some(incident),
        }
    }
    best
}

fn main() {
    let incidents = [
        Incident {
            name: "slow search",
            priority: 2,
        },
        Incident {
            name: "login outage",
            priority: 5,
        },
        Incident {
            name: "checkout outage",
            priority: 5,
        },
        Incident {
            name: "stale dashboard",
            priority: 1,
        },
    ];
    assert_eq!(next_incident(&incidents), Some(&incidents[2]));
    assert_eq!(next_incident(&incidents[..2]), Some(&incidents[1]));
    assert_eq!(next_incident(&incidents[..1]), Some(&incidents[0]));
    assert_eq!(next_incident(&[]), None);

    let quiet = [Incident {
        name: "cosmetic glitch",
        priority: 0,
    }];
    assert_eq!(next_incident(&quiet), Some(&quiet[0]));
}

Your solution

Standalone .rs / std only. Stored, not executed.

60 characters max. Not a login.

File overrides pasted code.

Maximum solution size: 64 KiB.

Editing access uses this browser's cookie. Clearing cookies loses access.