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.
A command-line tool reads its inputs from arguments, processes them, and exits with an exit code. env::args() is the entry point:
use std::env;
fn main() {
let argv: Vec<_> = env::args().collect();
match argv.get(1) {
Some(name) => println!("Guten Tag, {name}!"),
None => println!("Usage: guten-tag <name>"),
}
}
// run: cargo run -- Welt
// → Guten Tag, Welt!
// run without an argument: cargo run
// → Usage: guten-tag <name>
Three decisions are embedded in this example:
get returns an Option, and the None branch IS the usage outputLater you will learn about process::exit(code) — but only after you have printed the error message to stderr, so that the success path does not pollute stdout.
The CLI fundamentals
Check: Why argv.get(1) instead of argv[1] for the name?
Answer: argv[1] panics with a missing argument — get returns an Option
Index access is a contract with the runtime system; get() turns the danger into a handleable Option.
⚠ Common mistake: Printing the usage line with
println!to stdout wrecks pipelines — runguten-tag | grep -c Tagwithout an argument and the usage text suddenly counts as a match. Hints belong on stderr (eprintln!); stdout stays reserved for real results.
Takeaways:
A CLI without files is rarely useful. fs::read_to_string loads an entire file into memory as a String — and gives you the result as a Result, because file access can fail:
use std::env;
use std::fs;
fn main() {
let argv: Vec<_> = env::args().collect();
let pfad = argv.get(1).map(String::as_str).unwrap_or("notizen.txt");
match fs::read_to_string(pfad) {
Ok(inhalt) => println!("Zeilen: {}", inhalt.lines().count()),
Err(fehler) => eprintln!("Konnte '{pfad}' nicht lesen: {fehler}"),
}
}
// run: cargo run -- notizen.txt (file with 3 lines)
// → Zeilen: 3
// missing file: cargo run -- fehlt.txt
// → Konnte 'fehlt.txt' nicht lesen: No such file or directory (os error 2)
Note the three building blocks that Module 7 brings into play:
read_to_string returns Result — the error must not be swept under the rugunwrap_or provides a default path without paniceprintln writes the error message to stderr — stdout stays reserved for real results, so pipe partners work cleanlyAnd because a CLI user never wants to see your unwrapping internals: errors become messages, not panics. That is the application of error handling to the real world.
File I/O without pitfalls
Check: What does eprintln versus println express for an error case?
Answer: stderr instead of stdout — pipelines do not see the error as a result
stdout is the result line; diagnostics belong on stderr, so that grep and pipes are not polluted with error text.
⚠ Common mistake: Only calling
eprintln!in theErrarm and then falling through leaves the exit code at 0 — shell scripts and CI read the silent failure as success. After the message, the function must return early (or end with code 1) so callers can tell success and failure apart.
Takeaways:
Now the loaded text becomes the evaluation. Everything you learned in Module 10 meets the data of a file here:
fn main() {
let log = "info start\nerror disk voll\ninfo retry\nerror timeout\n";
let such = "error";
let mut treffer: Vec<&str> = log
.lines()
.filter(|z| z.contains(such))
.collect();
treffer.sort();
for zeile in treffer {
println!("{zeile}");
}
}
// → error disk voll
// → error timeout
Two abilities so far, and a third one added:
The collected lines are borrowed slices from the String — they live as long as the String. Since treffer stays in the same scope, this is safe and the borrow checker nods.
For a real tool, this becomes a search: suche:server shows only lines with "server". The suche function from the module codes does exactly that — filter, collect, done.
Data flow in the CLI
⚠ Common mistake: Extract the pipeline into a function that creates
logitself and the borrow checker blocks you: the return typeVec<&str>references a String that dies when the function ends — the compiler reportscannot return value referencing temporary value(E0515). Either return the String and the Vec together, or collect owned copies withto_string().
Takeaways:
A CLI thrives on clear structure. The proven division:
In the capstone code, this means: parse_befehl translates an argument into a command of the todo world — neu:TEXT appends, zeige shows all, suche:TEXT filters. The state is a Vec of notes; each line is rendered via a function that sets a checkmark or not.
Why this three-way split? Testability. Each stage is a pure function: arguments in, commands out — commands in, state out — state in, text out. You can test them individually without ever starting a program. That is exactly what the exercise makes clear.
Scenario: The todo tool that nobody sees crash anymore
What you take away from the course
⚠ Common mistake: Weaving parsing, processing, and output into a single
mainfunction makes no stage testable on its own — every change requires a full program run with matching arguments and files. Keepparse_befehland the render functions free of I/O so they stay pure functions, checkable with#[test].
Takeaways:
Course navigation: ← Module 12 · All 27 modules · Module 14 →