After a 26-day break from Rustlings (during which I turned 20, launched a conversion tool, bought myself a 2TB Crucial X9 SSD, and am preparing to upgrade my Mac mini M2 to the M4 next week), I decided to stretch my legs with a quick session.
Session Overview
Session 5 was refreshingly brief—just 30 minutes to complete Sections 14-15, covering generics and traits. After the absolute carnage of Session 4 ("I got 99 problems and all of them are the compiler screaming at me"), this felt like a gentle warm-up rather than a wrestling match with the type checker.
Generics: Making Code Flexible
Generics in Rust are about making your code work with multiple types without sacrificing type safety. The exercises started with the classic Vec problem—the compiler needs help figuring out what type to store when you're not immediately pushing concrete values.
The solution was straightforward: Vec<i16>
can handle both u8
and i8
values through the .into()
conversion. Simple enough.
The second exercise had me rewriting a wrapper struct to be generic over any type T
. Moving from a hardcoded positive integer wrapper to Wrapper<T>
felt natural—exactly the kind of abstraction that makes code reusable without losing clarity.
Traits: Shared Behaviour Across Types
Traits clicked quickly for me. They're essentially Rust's answer to interfaces in other languages, defining shared behaviour that different types can implement.
The AppendBar
trait exercises were quite enjoyable. Implementing it for String
was as simple as using format!("{}Bar", self)
. For Vec<String>
, it meant pushing "Bar" to the vector and returning the modified collection.
What I particularly appreciated was the default implementation concept in traits3.rs
. Being able to provide a fallback implementation in the trait definition itself ("Default license"
) while still allowing structs to override it if needed feels elegant. It reminds me a bit of C# interfaces with default methods.
The final two exercises dealt with trait bounds—specifying that generic functions can only work with types that implement certain traits. The syntax <T: Licensed>
and <T: SomeTrait + OtherTrait>
for multiple trait bounds is clean and explicit.
Reflection
This session was exactly what I needed—a gentle reintroduction to Rust after nearly a month away. Generics and traits are fundamental concepts that make Rust's type system both powerful and flexible, and the exercises did a good job of demonstrating their practical applications.
The 30-minute duration felt perfect for a "stretch session." Sometimes you don't need to marathon through complex ownership puzzles or wrestle with the borrow checker for hours. Sometimes you just need to remind your brain how the language thinks about abstraction and shared behaviour.
After the intensity of previous sessions, particularly Session 4's compiler battles, this felt like exactly the right pace. Quick, focused, and satisfying without being overwhelming.
Looking Forward
With generics and traits under my belt, I'm curious what's coming next in the Rustlings curriculum. These foundational concepts feel like they're building toward something more complex, and after this confidence-boosting session, I'm actually looking forward to finding out what that might be.
For now, though, I'm content with having knocked out two sections in half an hour and remembered why I enjoyed working through these exercises in the first place. Sometimes the best programming sessions are the ones that feel effortless.