Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Types and Error Handling  ›  Lesson

Enums: One of Several Shapes

Enums 12 minAn enum defines a type that is exactly one of several named variants, each able to carry its own data, and match handles every variant.

Enums: One of Several Shapes

A book in the library is on the shelf, or out on loan, or lost. Never two at once. A struct full of true/false flags can describe impossible mixtures; an enum cannot. Enums are also how Rust represents "a value or nothing" and "success or failure", which is why they belong in this section on errors.

The rule

An enum is exactly one variant at a time, and each variant carries its own data.

enum Status {
    OnShelf,
    OnLoan { card: u32, due_day: u32 },
    Lost,
}

let s = Status::OnLoan { card: 1042, due_day: 21 };
  • Variants are named with Type::Variant.
  • A variant can hold nothing (OnShelf), named fields (OnLoan { ... }), or unnamed ones like Damaged(String).
  • The data belongs to its variant: a book on the shelf simply has no card number.

Handle an enum with match, which must cover every variant and can pull out the data:

fn describe(s: &Status) -> String {
    match s {
        Status::OnShelf => String::from("available"),
        Status::OnLoan { card, due_day } => format!("with card {card} until day {due_day}"),
        Status::Lost => String::from("lost"),
    }
}

Enums get impl blocks just like structs, so describe could be a method, s.describe().

When you later add a variant, say Repair, every match without a catch-all stops compiling until you handle it. That is a feature: the compiler lists every place to update.

The shortcut

Ask of any piece of data: "Is this and or or?" Title and pages and status → struct. On shelf or on loan or lost → enum. Most real types combine both.

Designing the book recordStep 1 of 4
  1. The "and" part

    Every book has a title and pages. struct Book { title: String, pages: u32, status: Status }

The trap is modelling states as several bool fields. Two flags give four combinations, and some of them make no sense.

Drills

Answer fast — trust the rule.

quick drill

How many variants can one Status value be at the same moment?

quick drill

How do you create a Status value for a lost book?

quick drill

A match on Status has arms for OnShelf and OnLoan only. What happens?

quick drill

Why put card inside OnLoan rather than in Book?

Remember

  • An enum value is exactly one variant, and each variant carries only its own data.
  • "And" → struct, "or" → enum.
  • match must cover every variant, so adding one shows you every place to update.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainModel a book's status

A book is either on the shelf, on loan to a card number until a due day, or lost. Model this as an enum and explain why it is safer than a struct with fields on_loan: bool, lost: bool, card: u32 and due: u32.