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: 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.
Sometimes you need a small function in exactly one place — a comparison criterion, a bonus calculator, a callback. That's what closures are for: functions that capture their environment.
fn main() {
let basis = 7;
let verdopple_mit = |x: i32| x * basis;
println!("{}", verdopple_mit(6));
}
// → 42
The closure captures basis from the environment — without you having to declare it. The compiler infers types and decides HOW it is captured: by reference for reading, by reference for mutating, or — with move — by ownership. Closures that mutate their environment implement FnMut and need a mut binding:
fn main() {
let mut punkte = 0;
let mut punkte_dazu = |bonus: i32| punkte += bonus;
punkte_dazu(5);
punkte_dazu(3);
println!("Punkte: {punkte}");
}
// → Punkte: 8
The Three Closure Traits
move forces ownership instead of borrowing — required for threads (Module 12!)Try it out: build a closure that multiplies an input by your personal factor.
Check: Why does punkte_dazu need a mut binding when the closure mutates punkte?
Answer: A mutating closure implements FnMut — the call needs mut
Whoever changes captures is FnMut — and FnMut calls need a mut binding, just like &mut methods.
⚠ Common mistake: Forgetting what capture does: after
let greet = move || println!("Hi, {name}");, working withnameagain fails —movetransferred ownership into the closure, so the access reports E0382 ("use of moved value"). Withoutmovethe closure only borrows, but thennamemust not be dropped while the closure lives.
Takeaways:
Behind every for loop is an iterator — an object with a single method:
struct Zaehler {
bis: u32,
aktuell: u32,
}
impl Iterator for Zaehler {
type Item = u32;
fn next(&mut self) -> Option<u32> {
if self.aktuell < self.bis {
self.aktuell += 1;
Some(self.aktuell)
} else {
None
}
}
}
fn main() {
for n in Zaehler { bis: 4, aktuell: 0 } {
println!("{n}");
}
}
next() returns Some(Item) for each element and None at the end — that's the whole contract. If you implement Iterator for your type, you get all adapter methods from the standard library for free: map, filter, sum, collect, take, zip ... All ~70 methods share the same basis.
And importantly: iterators are lazy. (1..=1_000_000) doesn't compute anything yet — only a consumer drives the chain.
Check: What happens when you call next() on an exhausted iterator?
Answer: It returns None — and stays exhausted
None is the end signal. The for loop desugars to exactly this next loop with a break on None.
⚠ Common mistake: Forgetting
Noneinnext()— never signaling the end builds an infinite loop, because the desugared for loop waits forever for the end signal. The bug hides not in the loop body but in the method's termination check (aktuell < bisin theZaehlerabove).
Takeaways:
Now the payoff: adapters chain into pipelines — and no stage copies anything.
fn main() {
let zahlen = vec![1, 2, 3, 4];
let zehner: Vec<u32> = zahlen
.iter()
.filter(|z| *z % 2 == 0)
.map(|z| z * 10)
.collect();
println!("{zehner:?}");
}
// → [20, 40]
Three roles in the chain:
filter, map, take, cloned: only build the pipeline descriptioncollect, sum, count, max, any: drive next() until None comesThe latter is the insider tip: collect::<Result<Vec<u32>, String>>() returns either all values — or the first error. A pipeline of Results becomes a Result of a pipeline.
And because everything borrows slices instead of copying: the chain above doesn't consume zahlen — it borrows. An into_iter() would be the owning variant.
Check: What does (1..=5).map(|x| x * x).collect::<Result<Vec<u32>, String>>() return?
Answer: Ok(vec![1, 4, 9, 16, 25]) — no source for an error in the chain
Collect over Result: if the source yields Results, the whole chain becomes Ok(collection) or the first Err.
⚠ Common mistake: Dropping the
*beforezin the filter closure — the parameter arrives as&&u32, andz % 2 == 0fails with "no implementation for&&u32 % {integer}". Only*z % 2 == 0dereferences down to the value.
Takeaways:
First the scenario from everyday work — then the exercise:
Scenario: The log file in the console
What you achieved in Module 10
⚠ Common mistake: Calling
.unwrap()onmax_by_key— an empty log has no maximum, onlyNone, and.unwrap()then kills the program with a panic ("calledOption::unwrap()on aNonevalue").unwrap_oror amatchhandles exactly the empty case theOptionexists for.
Takeaways:
Course navigation: ← Module 9 · All 27 modules · Module 11 →