The Sorting Gate
The simulation is firing mock data logs at your terminal. You must build an iron-clad logic gate to categorize these logs by severity.
1. Defining Choices with Enums
If you use raw text strings to define system states (like "low" or "critical"), a simple typo ("crittical") can crash your entire server. Rust uses enum to enforce strict, predefined choices.
enum Severity {
Low,
Critical,
}
2. The Bulletproof Match
Rust provides match, an upgraded version of the traditional if/else block. The compiler forces you to handle every single variant inside an Enum. If you forget to write logic for a Critical alert, your program will refuse to compile. This guarantees you never miss a system failure.
enum Severity {
Low,
Critical,
}
fn main() {
let mock_log = Severity::Critical;
match mock_log {
Severity::Low => println!("Log ignored."),
Severity::Critical => println!("SYSTEM ALERT: Locking down simulation pod!"),
}
}
Note: Severity::Low and Severity::Critical are variants, not values like strings.
3. Triggering the Gate
What happens if we forget to write logic for the Critical state? Let's break the match block intentionally:
match mock_log {
Severity::Low => println!("Log ignored."),
// We deleted the Critical handler!
}
If you attempt to compile this code, Rust physically stops the build process and throws this exact error:
error[E0004]: non-exhaustive patterns: `Severity::Critical` not covered
This strict enforcement guarantees you never miss a system failure. You must handle every possibility.