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.
Rust allows you to redeclare a variable — the old binding is shadowed:
fn main() {
let x = 5;
let x = x + 1; // new binding, old is invisible from here on
println!("{x}"); // 6
// → 6
}
The subtle difference to mut: Shadowing creates a new binding that may also have a different type:
let spaces = " "; // &str
let spaces = spaces.len(); // usize — completely different type, perfectly legal
With mut, the type must stay the same. Shadowing is the idiomatic way to "transform a value step by step" without creating names like spaces2.
Shadowing ≠ Mutation
Shadowing: new binding, new type possible, old binding remains unchanged (until end of scope). mut: same binding, same type, value changes.
Check: What does this program output?
Answer: 6 — the second binding shadows the first
let creates a new binding; it hides the old one. No mut needed, no error.
⚠ Common mistake: Shadowing only applies within its own scope. If you write
let x = x + 1;inside a block and printxoutside it afterwards, you still get5— the outer binding was never touched. This compiles without any warning, so it's a genuine logic bug.
Takeaways:
i8–i128/u8–u128 (signed/unsigned), f32/f64, bool, char. The default is i32 and f64 respectively.
fn main() {
let a: i32 = -42;
let b: u8 = 255; // largest u8 value
let c = 2.5_f64;
let letter = 'z'; // char: ONE Unicode scalar point, single quotes
let s = "z"; // &str: string slice, double quotes
}
fn inc(x: u8) -> u8 {
x + 1
}
fn main() {
println!("{}", inc(255));
// → panic in a debug build: attempt to add with overflow
}
Type-correct — the compiler accepts it. But in debug builds, the program aborts at runtime with attempt to add with overflow; in release builds, it silently wraps to 0. Try it out locally!
fn main() {
let t = (1, 2.5, "three"); // tuple: mixed, fixed size
println!("{}", t.0); // 1 — access via dot index
let a = [1, 2, 3]; // array: homogeneous, stack, fixed size
println!("{}", a[1]); // 2 — runtime index check
}
Indexing is checked at runtime: a[5] compiles (when the index is a variable) but panics with index out of bounds — Rust never reads foreign memory.
Check: What happens with println!("{}", inc(255)) when inc(x: u8) -> u8 { x + 1 } — in a debug build?
Answer: Panic: attempt to add with overflow
In a debug build, Rust checks every arithmetic step for overflow and panics. In a release build, it silently wraps to 0. The compiler accepts the code — the error is at runtime.
⚠ Common mistake: You don't index tuples with square brackets:
t[0]fails withcannot index into a value of type ..., because a tuple isn't a uniform sequence. Access goes through dot notation —t.0,t.1— while square brackets with an index belong to arrays likea[0].
Rust has two common string types — the difference confuses beginners the most:
let literal: &str = "Hello"; // string slice: pointer + length, immutable
let owned: String = String::from("Hello"); // heap, mutable, owned by you
// alternative: "Hello".to_string()
let mut s = String::from("Hello");
s.push_str(", world"); // only String is mutable
println!("{}", s); // Display works for both
&str (a string slice) is a view onto existing characters — string literals live directly in the binary.String lives on the heap, is mutable, and is the standard choice for text you own.String::from("…") or "…".to_string() creates a String; &s or s.as_str() yields a &str.&str (flexible), return/own text as String.Check: Why does s.push_str fail on a string literal when s is declared as a plain let s = literal?
Answer: &str is immutable and not heap-based — push_str needs a mut String
String literals are &str (an immutable slice). For mutable text you need String::from(...) or .to_string().
Takeaways:
fn add(a: i32, b: i32) -> i32 {
a + b
}
No return, no semicolon: The last statement without a semicolon is the return value. The semicolon rule is the most important new habit for beginners:
fn broken(a: i32, b: i32) -> i32 {
a + b; // ❌ semicolon turns it into a statement of type ()
} // ❌ E0308: expected i32, found ()
Parameter types are mandatory, the return type comes after the arrow. () (unit) is Rust's "nothing" — the type of statements.
Semicolon = Discard value
expr; evaluates expr and discards the result (type ()). If you omit the semicolon, the expression is the value. This applies to blocks, if arms, and function bodies alike.
Check: Why does fn add(a: i32, b: i32) -> i32 { a + b } compile without return?
Answer: The last expression without a semicolon is the return value
Function bodies are blocks; the tail expression is their value. You only need return for early returns.
⚠ Common mistake: For
fn add(a: i32, b: i32) -> i32, callingadd(1.5, 2.0)doesn't compile — the compiler reportsexpected i32, found {float}. Rust never converts numbers silently the way C or JavaScript do. Either change the signature tof64or cast deliberately — note that1.5 as i32yields1: truncated, not rounded.
Takeaways:
fn main() {
let a = 3;
let b = 7;
let max = if a > b { a } else { b };
println!("{max}"); // 7
}
Both arms must have the same type — otherwise the compiler cannot determine the type of the whole expression. No parentheses around the condition, but curly braces are mandatory.
fn main() {
let n = 3;
let label = match n {
1 => "one",
3 => "three",
_ => "many",
};
println!("{label}"); // three
}
The compiler requires exhaustiveness: Every possible value must be covered by an arm. The wildcard arm _ catches the rest. If we forget it, it won't compile — an entire class of errors is handled at compile time.
fn main() {
let mut count = 0;
let result = loop {
count += 1;
if count == 10 {
break count * 2; // loop can yield a value
}
};
println!("{result}"); // 20
}
while and for (over ranges: for i in 0..5) complete the repertoire — but loop is the only loop that can yield a value as an expression.
Check: Why does match n without a _ arm not compile when not every value is covered?
Answer: Because the compiler requires exhaustiveness — exhaustive matching
The compiler requires every possible value to be covered by an arm. The _ arm catches the rest. Without it, match is not exhaustive — a compile error.
⚠ Common mistake:
let max = if a > b { a };without anelsedoesn't compile — as an expression, anifneeds a value for every possible path, and the false case would have none. The compiler tells you exactly that (if may be missing an else clause). Anifused purely as a statement, without producing a value, may of course skip theelse.
Takeaways:
Scenario: The data import
Course navigation: ← Module 1 · All 27 modules · Module 3 →