Learning path

Structs, enums and traits

Model data with structs and enums, match on patterns, handle Option and Result, and share behavior with traits and generics.

10 lessons · 14 challenges

  1. Lesson 1 challenge Structs Group related values into your own type with named fields.
  2. Lesson 2 challenges Methods and impl Blocks Attach functions to a struct with impl, including constructors.
  3. Lesson 1 challenge Enums Define a type that is one of several variants, each with its own data.
  4. Lesson 2 challenges Pattern Matching with match Compare a value against patterns with match, guards and if let.
  5. Lesson 1 challenge Temperature Converter Convert between Celsius and Fahrenheit with an enum.
  6. Lesson 2 challenges Option: Values That May Be Missing Represent a missing value with Option instead of null.
  7. Lesson 2 challenges Error Handling with Result Return errors as values with Result and pass them on with the ? operator.
  8. Lesson 1 challenge Bank Account with Result Model deposits and withdrawals that can fail with your own error type.
  9. Lesson 1 challenge Traits Describe shared behavior with a trait and implement it for different types.
  10. Lesson 1 challenge Generics Write one function or struct that works with many types.