Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Enums | Rust Fundamentals lesson | BraveProgrammer
  1. Courses
  2. /
  3. Rust Fundamentals

Lesson 14 of 50

·rust

Enums

15 minutes

Course progress28%

Enums

Enums (enumerations) represent a value that can be one of several distinct variants. They're incredibly powerful in Rust for expressing types with limited possibilities.

Basic Enums

enum Direction {
    North,
    South,
    East,
    West,
}

fn main() {
    let direction = Direction::North;
    
}
// Use it...

Enums with Associated Data

Enumvariants can carry data:

enum Message {
    Quit,
    Move { x: i32, y: i32 },
    Write(String),
    ChangeColor(i32, i32, i32),
}

fn main() {
    let msg1 = Message::Quit;
    let msg2 = Message::Move { x: 5, y: 10 };
    let msg3 = Message::Write(String::from(\"hello\"));
    let msg4 = Message::ChangeColor(255, 0, 0);
}

Matching Enums

Pattern matching with match is how you typically work with enums:

fn process_direction(dir: Direction) {
    match dir {
        Direction::North => println!(\"Going north!\"),
        Direction::South => println!(\"Going south!\"),
        Direction::East => println!(\"Going east!\"),
        Direction::West => println!(\"Going west!\"),
    }
}

fn main() {
    process_direction(Direction::North);  // Going north!
}

All variants must be handled.

Matching with Data

fn handle_message(msg: Message) {
    match msg {
        Message::Quit => println!(\"Quitting\"),
        Message::Move { x, y } => println!(\"Moving to ({}, {})\", x, y),
        Message::Write(text) => println!(\"Writing: {}\", text),
        Message::ChangeColor(r, g, b) => println!(\"Color: RGB({}, {}, {})\", r, g, b),
    }
}

The Option Enum

Rust provides Option<T> to represent nullable values:

enum Option<T> {
    Some(T),
    None,
}

It's so common, you don't need to import it:

fn divide(a: i32, b: i32) -> Option<i32> {
    if b == 0 {
        None
    } else {
        Some(a / b)
    }
}

fn main() {
    match divide(10, 2) {
        Some(result) => println!(\"Result: {}\", result),
        None => println!(\"Division by zero!\"),
    }
}

The Result Enum

For operations that can fail:

enum Result<T, E> {
    Ok(T),
    Err(E),
}
fn parse_number(s: &str) -> Result<i32, std::num::ParseIntError> {
    s.parse()
}

fn main() {
    match parse_number(\"42\") {
        Ok(n) => println!(\"Number: {}\", n),
        Err(e) => println!(\"Error: {}\", e),
    }
}

Convenience Methods

Option and Result have useful methods:

let x: Option<i32> = Some(5);
let y: Option<i32> = None;

// is_some() and is_none()
if x.is_some() {
    println!(\"x has a value\");
}

// unwrap() - panics if None
let val = x.unwrap();  // 5
// let bad = y.unwrap();  // panics!

// unwrap_or() - provides default
let val2 = y.unwrap_or(0);  // 0

// map() - transform the value
let doubled = x.map(|n| n * 2);  // Some(10)

Practical Example

enum Status {
    Active,
    Inactive,
    Pending { days_until: u32 },
}

fn process_status(status: Status) {
    match status {
        Status::Active => println!(\"Ready to go!\"),
        Status::Inactive => println!(\"Offline\"),
        Status::Pending { days_until } => {
            println!(\"Will activate in {} days\", days_until);
        }
    }
}
Make it stickPractice what you've learned1 quiz · 1 practical test · Optional activities from Rust FundamentalsBrowse activities
Previous lessonStructsContinue to lesson 15Pattern MatchingBack to Rust Fundamentals curriculum