Model structured data and serialization boundaries
Use structs/enums to represent payloads and records safely.
- Transform structured records into summary output with strong typing.
Real systems exchange structured payloads: API requests, events, configs, and reports. In Rust, clear structs and enums are the foundation for safe serialization workflows (often paired with Serde in production).
1#[derive(Debug)]
2struct Incident {
3 id: u32,
4 severity: u8,
5 resolved: bool,
6}
7
8fn main() {
9 let incidents = [
10 Incident { id: 1, severity: 5, resolved: true },
11 Incident { id: 2, severity: 9, resolved: false },
12 ];
13
14 let open_critical = incidents.iter().filter(|item| !item.resolved && item.severity >= 8).count();
15 println!("{}", open_critical);
16}1
Typed models reduce ambiguity: a u8 severity or explicit bool resolved state is harder to misuse than loosely shaped strings. This is crucial for safe data boundaries.
Try it
Up to 15 XPClassify fields by type role
Sort each field by the most appropriate Rust type category.
“severity score”
“resolved flag”
“incident id”
“service name”
Key takeaways
Transform structured records into summary output with strong typing.
Rust uses types and ownership rules to prevent common bugs.
Small experiments build intuition quickly.
Lesson quiz
5 questions · pass with 4 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Practice: write Rust
These run for real. Write the program, press Run tests, and read what the compiler tells you - learning to read compiler messages is half of learning Rust.
Summarize open incidents
Read lines in format id,severity,resolved separated by | and print how many incidents are both unresolved (0) and severity >= 8.
- Counts critical unresolved
- No critical unresolved
Rust lessons are authored for modern stable Rust; exercises run on Rust 1.82 via Wandbox, a free public service. Your source code and test input are sent to that service.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…