Week 4: Structs and Enums (Beginner)
Overview
Week 4 introduces Rust’s custom data types: structs and enums. These are Rust’s building blocks for creating rich, type-safe data structures and are comparable to classes and interfaces in JavaScript/TypeScript, but with Rust’s safety guarantees.
Day 1-3: Structs and Methods
Topics
- Defining and instantiating structs
- Field init shorthand
- Struct update syntax
- Tuple structs
- Unit-like structs
- Methods with
impl
blocks - Associated functions (like static methods)
- Builder pattern in Rust
- Deriving traits (
Debug
,Clone
, etc.) - Memory layout of structs
Resources
- Rust Book Ch 5
- Rust By Example: Structs
- Rustlings: Structs exercises
- Memory Layout of Rust Data Types
Use Cases
- Creating domain-specific data types
- Grouping related data together
- Encapsulating data and behavior
- Implementing object-oriented patterns in Rust
- Building reusable components
Day 4-7: Enums, Pattern Matching, Option/Result
Topics
- Defining enums
- Enum variants with data
- Methods on enums
- The
Option
enum:Some
andNone
variants- Replacing null values
- The
Result
enum:Ok
andErr
variants- Error handling
- Pattern matching with
match
- Advanced pattern matching:
- Destructuring
- Guards
- Binding
- The
if let
syntax - The
while let
syntax - Comparing with TypeScript union types
Resources
- Rust Book Ch 6
- Rust By Example: Enums
- Pattern Matching in Rust
- Option Enum Documentation
- Result Enum Documentation
- Rustlings: Enums exercises
Use Cases
- Representing states and variants
- Handling nullable values safely without null references
- Error handling without exceptions
- Creating type-safe state machines
- Domain modeling with exhaustive pattern matching
Exercises
- Create a
Rectangle
struct with width and height fields and methods to calculate area and perimeter - Implement a simple
Shape
enum with variants for different shapes and a method to calculate area - Build a simple address book using structs and enums
- Implement error handling using the
Result
type - Create a program that safely handles optional values using
Option
- Design a state machine using enums and pattern matching
Advanced Challenges
- Implement a simple vector library with structs and methods
- Build a configuration parser that handles errors gracefully with Result
- Create a command-line parser using enums for different commands
- Design a simplified JSON representation using Rust enums and structs
Next Steps
After completing Week 4, you’ll be able to design custom data types in Rust and handle errors safely using the Option and Result types. Week 5 will expand on error handling and introduce Rust’s collection types.