☼
⌃
☰

Enumerations

Table of Contents

A data type consisting of a fixed set of named constants (members or variants). Replaces magic numbers and strings with self- documenting, type-safe names. The specific power of enums varies significantly by language — Python enums are simple named constants; Rust enums are algebraic data types capable of holding associated data.

Related: Enums in Python Enums in Rust

Python

PYTHON
from enum import Enum, auto

class Direction(Enum):
    NORTH = "north"
    SOUTH = "south"
    EAST  = "east"
    WEST  = "west"

class Status(Enum):
    PENDING  = auto()   # auto-assigns integer values
    ACTIVE   = auto()
    INACTIVE = auto()

# Access
d = Direction.NORTH
d.name    # "NORTH"
d.value   # "north"

# Comparison
d == Direction.NORTH   # True
d is Direction.NORTH   # True

# Iteration
for direction in Direction:
    print(direction.name, direction.value)

# Type hint
def move(direction: Direction) -> None: ...

See Enums in Python for full detail.

Rust

Rust enums are sum types — each variant can hold different data. Combined with exhaustive match, they enforce handling every case at compile time.

RUST
// Simple enum
enum Direction { North, South, East, West }

// Enum with associated data (sum type / ADT)
enum Message {
    Quit,                       // no data
    Move { x: i32, y: i32 },   // named fields
    Write(String),              // tuple variant
    ChangeColor(u8, u8, u8),    // multiple values
}

// Exhaustive match — compiler error if a variant is missing
fn handle(msg: Message) {
    match msg {
        Message::Quit              => println!("Quit"),
        Message::Move { x, y }    => println!("Move to {x},{y}"),
        Message::Write(text)       => println!("Write: {text}"),
        Message::ChangeColor(r,g,b)=> println!("Color: {r},{g},{b}"),
    }
}

The two most important Rust enums are built into the language:

RUST
// Option<T> — replaces null
enum Option<T> { Some(T), None }

let name: Option<String> = Some("Alice".to_string());
let missing: Option<String> = None;

// Result<T, E> — error handling without exceptions
enum Result<T, E> { Ok(T), Err(E) }

fn parse_int(s: &str) -> Result<i32, std::num::ParseIntError> {
    s.parse::<i32>()
}

See Enums in Rust for full detail.

Why Enums Over Constants

PYTHON
# Magic numbers — fragile, opaque
status = 1
if status == 1:
    activate()

# String constants — typo-prone, no autocomplete
status = "active"
if status == "actve":   # silent bug
    activate()

# Enum — type-safe, self-documenting
status = Status.ACTIVE
if status == Status.ACTIVE:
    activate()

Tips

  • In Python, use auto() when the specific value does not matter and you just want distinct names
  • In Rust, reach for enums whenever you have a value that can be one of several distinct states — they are significantly more expressive than Python or Java enums
  • Option<T> and Result<T, E> are Rust's solution to null pointers and unchecked exceptions — both are enums, not special language constructs