↓ Skip to main content
  1. Rust/

Enums in Rust: From Syntax to Pattern Matching

677 words·4 mins· loading · loading · · ·Rust-basic

Introduction: What is an Enum?
#

Enums (Enumerations) in Rust are custom data types that allow you to represent a value that can be one of several possible variants.

If you have prior experience with Java or C++, you probably used enums to define simple sets of constants (such as days of the week). In Rust, however, enums are vastly more capable: each variant can hold heterogeneous data of different shapes and types, making your code safe, expressive, and concise.

Constructing Variants
#

Unlike many languages where enums are simply named integer constants under the hood, Rust enum variants can store rich data structures. A single enum can hold three distinct variant flavors:

  1. Unit variants: without associated data (similar to unit structs).
  2. Tuple variants: with unnamed payload data in parentheses (similar to tuple structs).
  3. Struct variants: with named fields inside curly braces {} (similar to traditional structs).

Let’s explore how variants are declared, what an uninhabited type represents, and how data is extracted using match in the interactive slides below:

Working with Enums in Rust
Step 1/3
 1#[allow(dead_code)]
 2#[derive(Debug)]
 3enum WebEvent {
 4    // Unit variant (no associated data)
 5    PageLoad,
 6    // Tuple variant (with unnamed associated data)
 7    KeyPress(char),
 8    // Struct variant (with named fields)
 9    Click { x: i64, y: i64 },
10}
11
12fn main() {
13    let load = WebEvent::PageLoad;
14    let press = WebEvent::KeyPress('q');
15    let click = WebEvent::Click { x: 320, y: 240 };
16
17    println!("load: {:?}", load);
18    println!("press: {:?}", press);
19    println!("click: {:?}", click);
20}

Enum Variant Construction

  • Enums can combine different data shapes under a single unified type.
  • Unit variants carry no associated data (PageLoad).
  • Tuple variants hold unnamed data inside parentheses (KeyPress).
  • Struct variants hold named fields inside curly braces (Click).

Implementation Details:
#

  • Step 1. Variant Flavors: The WebEvent enum demonstrates all three variant forms. An important detail: all fields and payloads defined within enum variants are automatically public; you do not need to prefix them with pub.
  • Step 2. Empty Enums (Uninhabited Types): Rust allows defining an enum without any variants at all (e.g., enum EmptyEnum {}). It is impossible to instantiate this type in safe code. Such types help represent states that should never occur, allowing the compiler to optimize unreachable execution paths.
  • Step 3. Extracting Data (Pattern Matching): Because different variants can hold entirely different data types, the compiler will not allow direct access without inspection. We must determine which variant is currently active. The most expressive way to do this is using the match expression.

Important: The match expression in Rust is exhaustive. The compiler will produce an error if even a single enum variant is left unhandled.


Test Your Knowledge
#

Take a quick interactive quiz to solidify your understanding of Rust enums:

Article read
Please rate how helpful and clear this article was to you
▶ Article series
About Rust - This article is part of a series.

Related