Lesson 03 · Backend & Systems with Rust
Rust: Structs, Enums, Traits & Errors
Rust's modeling toolkit: structs with impl blocks and associated functions, enums whose variants carry data, exhaustive pattern matching, traits and generics with derive, and error handling built on Result, the ? operator, and custom error types.
Prerequisites: Rust: Ownership & Borrowing
Learning objectives
By the end of this lesson, you will be able to:
- Define structs with impl blocks: methods that borrow &self or &mut self, and associated functions called on the type itself - the pattern behind String::from
- Model choices with enums whose variants carry data, reading values out with exhaustive match patterns and bindings
- Explain Option<T> and Result<T, E> as ordinary standard-library enums and handle them with match, if let, and while let
- Define traits with required and default methods, implement them for your types, and request compiler-generated impls with derive
- Write generic functions with trait bounds and propagate errors with the ? operator over a custom error type implementing Display and std::error::Error