keyword

Rust enum: model a value with distinct variants

An enum defines a type whose values belong to one of several named variants. Variants may carry different data, which is useful for modeling states. Expected output (not executed): Parcel 42 Why: The match selects Sent and binds its tracking number. Common mistake: An enum value does not hold every variant at once.

Formal Syntax & Structure

rust syntaxrust
enum Name { First, Second(Type) }

Working Example

Example Snippetrust
enum Delivery { Pending, Sent(u32) }
fn main() {    let status = Delivery::Sent(42);    match status {        Delivery::Pending => println!("Waiting"),        Delivery::Sent(id) => println!("Parcel {id}"),    }}
Rust Reference