100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Rust

Enums in Rust

Enums define a type by enumerating its possible variants, each of which can optionally hold its own data.

Data StructuresBeginner9 min readJul 8, 2026
Analogies

Introduction

An enum (enumeration) defines a type by listing its possible variants. Unlike C-style enums, which are just a set of named integer constants, Rust enums are far more powerful: each variant can hold its own data of different types and amounts. This makes enums ideal for modeling values that can be one of several distinct, well-defined shapes, such as an IP address that is either IPv4 or IPv6.

🏏

Cricket analogy: Rust's enum is like classifying a dismissal type — bowled, caught, or run out — where a C-style enum would just be a numbered scoreboard code, but Rust's caught variant can also carry who the fielder was.

Syntax

rust
enum IpAddr {
    V4(String),
    V6(String),
}

let home = IpAddr::V4(String::from("127.0.0.1"));
let loopback = IpAddr::V6(String::from("::1"));

Explanation

Each variant of IpAddr is a constructor for the enum type, and here both V4 and V6 hold a String. Variants aren't required to hold the same type or amount of data — one variant could hold a struct-like set of named fields, another could hold nothing at all. Enums pair naturally with the match expression, which forces you to handle every variant, making illegal states easier to avoid. Two of the most important enums in the standard library, Option<T> and Result<T, E>, are themselves ordinary enums (Option has Some(T) and None; Result has Ok(T) and Err(E)) — they are covered in depth in their own topics.

🏏

Cricket analogy: IpAddr::V4(String) is like a wicket-type constructor — Bowled(batsman_name) — each variant builds a specific kind of value; match then forces you to handle stumped, caught, and run-out cases, so none is missed.

Example

rust
enum IpAddr {
    V4(String),
    V6(String),
}

fn describe(addr: &IpAddr) -> String {
    match addr {
        IpAddr::V4(s) => format!("IPv4 address: {}", s),
        IpAddr::V6(s) => format!("IPv6 address: {}", s),
    }
}

fn main() {
    let home = IpAddr::V4(String::from("127.0.0.1"));
    let loopback = IpAddr::V6(String::from("::1"));

    println!("{}", describe(&home));
    println!("{}", describe(&loopback));
}

// Output:
// IPv4 address: 127.0.0.1
// IPv6 address: ::1

Key Takeaways

  • Enums list the possible variants a value of that type can take.
  • Unlike C-style enums, Rust variants can each hold different types and amounts of data.
  • Enum variants are constructed with the Enum::Variant(...) syntax.
  • match is the idiomatic way to handle enums and ensures every variant is covered.
  • Option<T> and Result<T, E> are standard library enums built on this same mechanism.

Practice what you learned

Was this page helpful?

Topics covered

#Rust#RustProgrammingStudyNotes#Programming#EnumsInRust#Enums#Syntax#Explanation#Example#StudyNotes#SkillVeris