Enums with data
What you'll learn:
- declare an enum whose variants carry different data;
- keep a value that may be missing in an
Optioninstead of null; - take values apart with
matchand predict which arm runs; - write short checks with
if let,let elseand patterns with ranges and conditions.

Picture a post office. Every item carries a sticker with its kind: "letter", "package" or "tube". Inside lies what that kind needs: a letter has nothing to weigh, a package has its weight written down, a tube has its length. The sorter looks at the sticker first and only then decides what to do with the contents.
How do you keep such an item in one variable? A struct from the previous theme gathers fields "all at once": kind, weight and length. But then a letter gets a meaningless weight and length, and kind can hold 7 even though there are only three kinds. You need a type for "one of several", and Rust has one:
enum Parcel {
Letter,
Package(u32),
}
fn main() {
let parcels = [Parcel::Letter, Parcel::Package(3)];
for p in parcels {
match p {
Parcel::Letter => println!("letter"), // letter
Parcel::Package(kg) => println!("{kg} kg"), // 3 kg
}
}
}Let's walk through the example line by line:
enum Parcel { … }describes two kinds of items: a letter with no data and a package with a weight of typeu32.Parcel::LetterandParcel::Package(3)create two items. Both are values of the same typeParcel, so they sit in one array.for p in parcelstakes each item in turn.match plooks at the sticker. For the letter it printsletter; for the package the weight goes into the variablekg, and it prints3 kg.
Declaring an enum
An enum is a type whose value is always exactly one of the variants listed in advance. The declaration has these parts:
enumis the keyword: "I am declaring an enum";Parcelis the type name inCamelCase;- the variants go in curly braces, separated by commas, also capitalized.
You refer to a variant through the type name and ::: Parcel::Letter. You have already called String::from and Self::new the same way. A Parcel value is never "a bit of a letter and a bit of a package": it holds exactly one variant.
Three kinds of variants
Variants resemble the three kinds of structs from the previous theme, and one enum can mix them:
enum Parcel {
Letter,
Package(u32),
Tube { cm: u32 },
}No data
Letter carries nothing but the sticker itself, like a unit struct. You create the value with its name alone: Parcel::Letter.
Tuple variant
Package(u32) stores data in order, without names, like a tuple struct. You create the value the way you call a function: Parcel::Package(3). The parentheses can hold several values: Rect(u32, u32).
Variant with named fields
Tube { cm: u32 } stores named fields, like an ordinary struct. You create the value with a literal: Parcel::Tube { cm: 90 }.
The data sits inside
You read a struct field with a dot. An enum does not allow that: p.0 gives error[E0609]: no field `0` on type `Parcel` . The compiler does not know in advance whether p holds a package or a letter, and a letter has no weight at all.
So you check the variant first. match works like a sorting machine: the line Parcel::Package(kg) => … accepts only packages and puts their weight into kg. You cannot reach a letter's weight, because it does not exist.
Here the post office picture falls short. A real box with a "letter" sticker can be stuffed with anything. In Rust a variant and its data cannot be separated: Letter has no room for a weight at all, and Package always holds a u32.
How this differs from C
Interviewers like to ask this: "How does an enum in Rust differ from an enum in C?" The answer:
- in C an enum is a set of named integers, and you cannot attach data to them;
- in Rust each variant can carry its own data, different for different variants;
- a value is always exactly one variant, and you reach its data only by checking the variant;
- a struct describes "all at once", an enum describes "one of".
Common mistake
The variant is written without the type name:
enum Parcel {
Letter,
Package(u32),
}
fn main() {
let p = Package(3); // ✗
}error[E0425]: cannot find function, tuple struct or tuple variant `Package` in this scopeThe program does not compile: the name Package lives inside Parcel, and the compiler does not find it on its own. The fix is the full name Parcel::Package(3).
Key points
- An enum is a "one of" type: a value is always exactly one variant.
- A variant has no data, is a tuple variant
Package(u32), or has named fieldsTube { cm: u32 }. - You refer to a variant with
:::Parcel::Letter; without the type name you get E0425. - A dot does not read a variant's data: you check the variant first with
match.