Rust: ownership, borrowing and traits
You grasp ownership and borrowing, the heart of Rust's safety model, and write reusable code with traits, generics and iterators.
Level 201 · 19 lessons · 18 practice labs
Lessons
Ownership
- Stack, heap and the ownership rules
- Move, Copy and Clone
- Borrowing: & and &mut
- Reading borrow checker errors
- Slices: a safe view into part of a sequence
- Lifetimes: what 'a tells the compiler
- Short task: fix five programs that will not compile
Traits and generics
- Defining and implementing traits
- Generics and trait bounds
- Debug, Clone and PartialEq with derive
- Dynamic dispatch with dyn Trait
- Operator traits and From, Into
- Short task: notification channels with a shared interface
Iterators and smart pointers
- Closures and the Fn traits
- Iterators: map, filter, collect
- Box, Rc and RefCell
- Error types with thiserror and anyhow
- Short task: report an inventory with an iterator chain
Capstone
- Capstone: a trait-based, tested record store
Practice labs
- Writing Display and FromStr for a colour type: Implement Display and FromStr for a type holding a hex colour code so that printing and parsing back gives the same value.
- Building a validated type with TryFrom: Build a newtype that accepts only valid postcodes with TryFrom, derive Ord and Hash and show by test that an invalid value cannot be constructed by any route.
- Fixing a borrow error: Make code that pushes to a vector while holding a reference to it compile by reorganising the scopes.
- Removing needless clone calls: Rewrite report code that clones everywhere using &str and & borrows so the tests pass with zero clone calls.
- Splitting two mutable borrows: Make a function that wants to change two elements of an array at once compile and work correctly with split_at_mut.
- Slicing the first word of a command line: Return the first word of a command line as a &str slice without allocating a new String.
- Returning the longest line with a lifetime: Add the right lifetime parameter to a function that returns the longer of two strings and clear the compile errors.
- A parser struct that holds a reference: Write a Tokenizer struct that holds a reference to its input text and returns tokens as slices without copying.
- Defining an area trait for shapes: Implement a shared Area trait for circle, rectangle and triangle and pass the tests that compute total area.
- A generic maximum with trait bounds: Write a generic max_of that works for any type with PartialOrd and Copy bounds, returning None for an empty slice.
- Implementing From and Add for a money type: Implement From<u64> and Add for Defne Finans's cents-based Money type, rejecting additions across currencies.
- Choosing an exporter with dyn Trait: Collect CSV and JSON exporters in a Box<dyn Exporter> list and run the right one for the requested format.
- Filtering orders with an iterator chain: Rewrite a loop that totals orders as a filter, map and sum chain with the same result.
- Writing a closure that keeps state: Produce an FnMut closure that returns an increasing counter and use it in retry logic.
- An iterator over paged results: Implement the Iterator trait for a source that returns data page by page, stopping cleanly after the last page.
- A shared counter with Rc and RefCell: Build a structure where several task objects write to a shared counter using Rc<RefCell<..>> without borrow panics.
- Defining your own error type: Define an enum with thiserror for a config reader that separates missing-field and invalid-value errors.
- A recursive expression tree with Box: Define an expression tree of add and multiply nodes with Box, evaluate it and return division by zero as an error.
About this course
You grasp ownership and borrowing, the heart of Rust's safety model, and write reusable code with traits, generics and iterators.
What you will learn
- Ownership
- Traits and generics
- Iterators and smart pointers
- Capstone
Prerequisites
Finishing the level 101 courses of the same path, or knowing those topics, is enough.
Lab environment
Lessons run in your own Kali Linux terminal that opens in the browser. Nothing to install; the lab starts clean in every lesson.
Frequently asked questions
- Is this course right for my level?
- The course is at level 201; it assumes you know the topics of the lower-level courses.
- Is there anything to install?
- No. Every lesson runs in your own lab environment that opens in the browser.
- How do lessons work?
- Each lesson has 8 to 10 steps and a short quiz. Early steps show the command to type, later steps give less help; if two attempts fail, the solution opens.
- Can I try it for free?
- Yes. The first three lessons of every course are free.