Enums
An enum is a type with a closed list of variants. Each variant may carry its own data. A value is exactly one variant at a time, and match must cover them all.
Detailed Explanation
A C enum is a named integer. A Rust enum is a tagged union: a tag that says which variant you have, plus the data that variant stores. enum IpAddr { V4(u8, u8, u8, u8), V6(String) } is one type with two shapes. A value is either the four-byte form or the string form, never both, never neither.
This is the type you want for any value that is "one of a known set of cases". Status codes with payloads, a parser's tokens, Option and Result themselves — those last two are ordinary enums in core. None is a variant. Some(T) is a variant that holds a T. There is no null.
Variants can be unit-like (Ready), tuple-like (Failed(String)), or struct-like (Blocked { retries: u8 }). You construct them with the variant name: Status::Ready, Status::Failed(e.to_string()). If the enum is in scope via use Status::*;, you can drop the prefix, which is convenient in match arms and a little dangerous in large modules.
The compiler lays an enum out as a discriminant plus the largest variant, with room for niche optimisation. Option<&T> is the same size as &T because None uses the null pointer niche. Option<bool> is one byte. You do not get that for free with a hand-rolled struct { present: bool, value: T }, which is always bigger and allows present == false with a garbage value.
Exhaustiveness is the reason to prefer an enum over a string or a boolean flag. A bool is_failed plus a String message can be is_failed == false with a leftover message. An enum cannot represent that state. Adding a variant is a breaking change for every match that does not have _, which is what you want for a public enum you expect callers to handle, and why #[non_exhaustive] exists when you want to add variants later without breaking crates.
When you reach for an enum: a closed set of cases, especially when cases carry different data. When you do not: an open set that comes from the network as text you do not control — parse into an enum at the boundary, keep a catch-all variant if you must tolerate unknowns.
Code Examples
enum Status {
Ready,
Blocked { retries: u8 },
Failed(String),
}
fn retry_after(status: &Status) -> u8 {
match status {
Status::Ready => 0,
Status::Blocked { retries } => *retries,
Status::Failed(_) => 0,
}
}
fn main() {
let status = Status::Blocked { retries: 2 };
println!("{}", retry_after(&status)); // prints: 2
}Common Gotchas
A new variant breaks every exhaustive `match`
That is the point for domain enums. Use `#[non_exhaustive]` on public enums you intend to extend, which forces a `_` arm downstream.
C-like discriminants are not the whole enum
`enum Color { Red = 1 }` can be cast with `as u8`, but an enum with data cannot. Do not treat Rust enums as integers by habit.
`Option` and `Result` are enums
`None` is not a keyword. Matching them is ordinary pattern matching, not a special null test.
A boolean plus optional payload is a worse enum
`failed: bool` and `message: String` allow impossible combinations. Make `Failed(String)` a variant.