Lesson 04 · Backend & Systems with Rust
Rust: Collections, Generics & Lifetimes
The data structures real Rust programs run on - Vec, HashMap, and lazy iterator chains with map, filter, and collect - plus an honest look at String's UTF-8 bytes and the lifetimes that keep borrowed references provably valid.
Prerequisites: Rust: Structs, Enums, Traits & Errors
Learning objectives
By the end of this lesson, you will be able to:
- Use Vec<T> with push, pop, and iteration, choosing deliberately between indexing (panics out of bounds) and get (returns an Option)
- Explain String's UTF-8 layout: len() counts bytes, chars() counts scalar values, integer indexing never compiles, and mid-character slices panic
- Build HashMaps with insert, get, and the entry API's single-lookup check-and-mutate idiom
- Distinguish iter(), iter_mut(), and into_iter() by what each borrows or takes, and chain lazy adapters into collect or sum with the destination type spelled out
- Read and write function signatures with lifetime annotations, stating what they constrain and what the elision rules infer for you