By the end of this lesson
Represent a small set of valid states with an enum.
Before you start
- Rust 1.90.0 or later, a terminal and a plain-text editor. Examples were tested with Rust 1.90.0, Edition 2024 on Linux.
- Complete lesson 14, or be comfortable explaining and running its examples.
Three words to know
- Enum
- A type whose values are one of its variants.
- Variant
- One possible enum case.
- Pattern
- A form matched against a value.
1. Understand the idea
An enum lets the type describe the permitted states. This avoids passing arbitrary strings around after validation. match checks variants and requires exhaustive coverage; adding a variant can reveal every place needing a new branch. A wildcard is useful when deliberately grouping cases, but it can hide future omissions if used everywhere.
2. Save, run and observe
Save this complete program as lesson.rs in a fresh practice folder. It replaces that lesson file only; keep earlier examples separately. Compile it with rustc, then run the executable. Rust source goes in the editor; rustc and cargo commands go in the terminal.
enum Status { Open, Closed }
fn label(status: Status) -> &'static str {
match status { Status::Open => "open", Status::Closed => "closed" }
}
fn main() {
println!("{} {}", label(Status::Open), label(Status::Closed));
}
What you should see · example output
open closed
rustc --edition=2024 lesson.rs -o lesson
./lesson3. Explain the result
The function consumes Status here; the small enum is not declared Copy. Both variants are handled explicitly. The returned &'static str points to a string literal, not to temporary local data. Later we will parse text into validated states before counting.
YOUR TURN
A small check before you continue
If you add a Pending variant, what must change in label?
Show a suggested answer
Add a branch handling Pending, or an intentional grouping. The current match is no longer exhaustive.
Sources and scope
Documentation and examples reviewed 2026-10-18. Provider screens, package versions and output wording can differ. Examples show expected outcomes; they are not a claim that OpenViST connected to your server or verified your setup.
