0Pricing
Learn Rust Coding · Lesson

Defining Your First Enum

Create simple enumerated types.

Defining Your First Enum is a free Learn Rust Coding lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn Rust Coding learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is an Enum?

An enum (enumeration) lets you define a type by listing its possible values, called variants. A value of the type can only ever be one of those variants.

Enums shine when something has a fixed set of states, like the cardinal directions or the suits in a deck of cards.

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

Declaring an Enum

You declare an enum with the enum keyword, a name, and a comma-separated list of variants inside curly braces.

By convention enum names use PascalCase (like Direction), and so do the variant names (like North).

enum TrafficLight {
    Red,
    Yellow,
    Green,
}

Creating an Enum Value

To build a value you write the enum name, two colons ::, then the variant.

For example, Direction::North creates a value of type Direction. The :: tells Rust which enum the variant belongs to.

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

fn main() {
    let heading = Direction::East;
    println!("Created a direction value!");
}

Enums Are Types

Once defined, an enum is a real type just like i32 or bool. You can store its values in variables and pass them to functions.

A function that takes a Direction will accept any of its variants, but nothing else.

enum Direction {
    North,
    South,
}

fn describe(d: Direction) {
    println!("Got a direction");
}

fn main() {
    describe(Direction::North);
}

Why Not Just Use Numbers?

You could represent a traffic light with 0, 1, and 2, but nothing stops you from accidentally using 7.

An enum makes invalid states impossible: the compiler guarantees the value is always one of the listed variants. This is safer and far easier to read.

Deriving Debug to Print

By default Rust does not know how to print an enum. Adding #[derive(Debug)] above the enum lets you print it with the {:?} formatter.

This is great for quick debugging while you learn.

#[derive(Debug)]
enum Direction {
    North,
    South,
}

fn main() {
    let d = Direction::South;
    println!("{:?}", d);
}

Comparing Enum Values

Add #[derive(PartialEq)] so you can compare variants with ==.

This is handy for simple checks, though for richer behavior the match expression (coming soon) is usually the better tool.

#[derive(PartialEq)]
enum Light {
    Red,
    Green,
}

fn main() {
    let l = Light::Red;
    if l == Light::Red {
        println!("Stop!");
    }
}

Many Variants Allowed

An enum can have as many variants as you need. They are all part of the same type.

Listing related states together keeps your code organized and makes the possible options obvious at a glance.

enum Suit {
    Hearts,
    Diamonds,
    Clubs,
    Spades,
}

Enums in the Standard Library

Rust's own standard library uses enums everywhere. Two famous ones are Option (a value that may be present or absent) and Result (success or error).

Learning enums now prepares you to understand these core types later.

// Option is an enum in std:
// enum Option<T> {
//     Some(T),
//     None,
// }

Copying Simple Enums

Enums whose variants hold no data can derive Clone and Copy. Then values are copied automatically instead of moved.

This makes small state enums behave like simple values such as integers.

#[derive(Clone, Copy)]
enum Coin {
    Heads,
    Tails,
}

fn main() {
    let a = Coin::Heads;
    let b = a; // a is still usable
}

Putting It Together

Here is a complete program: define an enum, create a value, and print it using Debug.

Run it and try changing Green to another variant to see the output update.

#[derive(Debug)]
enum TrafficLight {
    Red,
    Yellow,
    Green,
}

fn main() {
    let current = TrafficLight::Green;
    println!("Light is {:?}", current);
}

Quick Check

Test your understanding of enum basics.

Recap

You learned that an enum defines a type from a fixed list of variants, declared with the enum keyword.

You create values with EnumName::Variant, and you can derive Debug, PartialEq, or Copy to print, compare, and copy them. Next you will learn to react to each variant with match.

Frequently asked questions

Is the “Defining Your First Enum” lesson free?

Yes — the full text of “Defining Your First Enum” is free to read here on the web, and the Learn Rust Coding course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn Rust Coding course, upgrade to CoddyKit PRO.

What will I learn in “Defining Your First Enum”?

Create simple enumerated types. You practise Learn Rust Coding with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Learn Rust Coding?

No prior experience is required. Learn Rust Coding on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Defining Your First Enum” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Learn Rust Coding lesson?

Yes. Every Learn Rust Coding lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Defining Your First Enum
  2. Matching on Enum Variants
  3. Enums with Data
  4. Match Guards and Bindings
← Back to Learn Rust Coding