Deep Dive: egui/eframe for instant UIs — why Immediate Mode fits system languages, State management with Rust's ownership, Layout & Style, Custom painting, and when Retained Mode is the better choice.
Deep dive: atomic operations, memory orderings, and CAS loops — tools without locks but with sharper contracts. When atomics shine and when the mutex stays.
In Module 5 you learned: Result as a label "can go wrong". So far the error part was often just a String — quick, but unstructured. A custom error enum turns each error type into a named value that callers can react to specifically:
#[derive(Debug, PartialEq)]
enum ParseError {
InvalidLine(String),
NotANumber(String),
}
fn parse_line(line: &str) -> Result<(String, u32), ParseError> {
let (key, value) = line
.split_once('=')
.ok_or_else(|| ParseError::InvalidLine(line.to_string()))?;
let num = value
.trim()
.parse::<u32>()
.map_err(|_| ParseError::NotANumber(line.to_string()))?;
Ok((key.trim().to_string(), num))
}
fn main() {
match parse_line("timeout=30") {
Ok((key, value)) => println!("{key} = {value}"),
// → timeout = 30
Err(e) => println!("Fehler: {e:?}"),
}
}
Two things happen here: ok_or_else converts the Option from split_once into a Result with our error type, and map_err translates the foreign error of the parser (ParseIntError) into our enum. In the end, the whole function has exactly one error type.
Why not String as error type?
assert_eq! with PartialEq)Check: What does a caller gain from the error enum compared to a String error?
Answer: He can react differently per variant — and doesn't forget any due to exhaustiveness
Typed errors make the reaction explicit: match on each variant, and the compiler complains if one is missing.
⚠ Common mistake: Forgetting to derive
PartialEqfor your error enum — thenassert_eq!in tests won't work because comparison is not implemented.
Takeaways:
Functions that can't do anything meaningful with an error themselves pass it upward. With match this becomes a match pyramid — with the ? operator it's one line per step:
fn parse_number(s: &str) -> Result<i32, String> {
s.trim()
.parse::<i32>()
.map_err(|_| String::from("keine Zahl"))
}
fn parse_pair(s: &str) -> Result<(i32, i32), String> {
let (a, b) = s
.split_once(',')
.ok_or_else(|| String::from("Komma fehlt"))?;
let x = parse_number(a)?;
let y = parse_number(b)?;
Ok((x, y))
}
fn main() -> Result<(), String> {
let (x, y) = parse_pair("3,4")?;
println!("Paar: {x}, {y}");
// → Paar: 3, 4
Ok(())
}
The ? means: unpack the Ok value and continue — or return the error immediately from the function. And even main may return Result: on Err the program exits with a non-zero exit code and prints the error — exactly what scripts and CI expect from a CLI tool.
The three ? rules
? converts automatically via From — or you translate beforehand with map_err? is for propagating; the decision (report, retry, abort) is made at the topTry it yourself: write a function doppelt(eingabe: &str) that parses the input as a number, doubles it, and returns it as a Result — with ? instead of match.
⚠ Common mistake: Using
?in amain()function that returns()— the compiler complains because it cannot propagate the error. Solution:fn main() -> Result<(), E>.
Takeaways:
Rust knows two error worlds: Panic (irreversible: the program aborts) and Result (recoverable: the caller decides). The rule of thumb in practice:
fn lade_konfig(pfad: &str) -> Result<String, String> {
match std::fs::read_to_string(pfad) {
Ok(inhalt) => Ok(inhalt),
// → Ok("timeout=30\nretries=5")
Err(e) => Err(format!("Konfiguration nicht lesbar: {e}")),
}
}
fn main() {
match lade_konfig("app.conf") {
Ok(inhalt) => println!("{} Bytes geladen", inhalt.len()),
Err(msg) => {
eprintln!("Start abgebrochen: {msg}");
std::process::exit(1);
}
}
}
.expect("Kontext") is the more honest brother of .unwrap(): on panic it prints the context — but both do not belong on paths whose failure you expect. If it does crash, this helps:
RUST_BACKTRACE=1 ./meinprogramm
The backtrace shows the call stack at the moment of panic — the trail from the broken place back to you.
Check: A user enters abc as age. Panic or Result?
Answer: Result — invalid user input is an expected, handleable case
Expected errors → Result. Panic is for states where your code lied — not for user input.
⚠ Common mistake: Calling
.unwrap()on user input — the program crashes on every typo. User errors always belong in Result, never in Panic.
Takeaways:
Everything from the module in one building block: a parser for key=value lines that reports errors typed and never panics. The scenario:
Scenario: The server configuration
The exercise builds exactly this parser — from the enum to the output loop:
⚠ Common mistake: Many forget to handle empty files in the config parser: trying to parse the first line when the file is empty causes a panic. Always check the length first or use
lines().next().
What you accomplished in Module 7
Takeaways:
Course navigation: ← Module 6 · All 27 modules · Module 8 →