Bird
Raised Fist0
Rustprogramming~10 mins

Enum variants in Rust - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Enum variants
Define Enum with Variants
↓
Create Variable of Enum Type
↓
Assign a Variant to Variable
↓
Use Variable in Match or Logic
↓
Access or Use Variant Data
↓
End
This flow shows how to define an enum with variants, create a variable of that enum type, assign a variant, and then use it.
Execution Sample
Rust
enum Color {
    Red,
    Green,
    Blue(u8),
}

fn main() {
    let c = Color::Blue(128);
}
Defines an enum Color with three variants, then creates a variable c with the Blue variant holding a value 128.
Execution Table
StepActionCode LineVariable StateNotes
1Define enum Color with variants Red, Green, Blue(u8)enum Color { Red, Green, Blue(u8) }No variables yetEnum type created
2Create variable c and assign Blue variant with value 128let c = Color::Blue(128);c = Blue(128)Variable c holds Blue variant with data 128
3Use variable c (example: match or print)match c { ... }c = Blue(128)Can access variant and its data
4End of examplec = Blue(128)Execution stops here
💡 Variable c assigned Blue variant with value 128; program ends after assignment.
Variable Tracker
VariableStartAfter Step 2Final
cundefinedBlue(128)Blue(128)
Key Moments - 3 Insights
Why do some enum variants have data like Blue(u8) while others don't?
Variants can be simple names (like Red) or hold data (like Blue(u8)). Step 1 shows the enum definition with both kinds.
How do we assign a value to a variant with data?
Step 2 shows assigning Blue(128) to variable c, which means Blue variant holds the number 128.
Can we use the variable c to get the data inside Blue variant?
Yes, step 3 shows that you can match on c to access the 128 inside Blue(128).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of variable c after step 2?
ABlue(128)
BGreen
CRed
Dundefined
💡 Hint
Check the 'Variable State' column in row for step 2 in execution_table.
At which step is the enum Color defined?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Action' and 'Code Line' columns in execution_table for step 1.
If we changed the assignment to let c = Color::Red;, how would the variable_tracker change after step 2?
Ac would be Blue(128)
Bc would be Green
Cc would be Red
Dc would be undefined
💡 Hint
Variable_tracker shows the value of c after step 2; changing assignment changes that value.
Concept Snapshot
Enum variants define different named options for a type.
Variants can be simple names or hold data.
Use EnumName::Variant to create a value.
Example: enum Color { Red, Green, Blue(u8) }.
Assign with let c = Color::Blue(128);.
Access variant data with match or if let.
Full Transcript
This example shows how to define an enum named Color with three variants: Red, Green, and Blue which holds a number. We then create a variable c and assign it the Blue variant with the number 128. The execution table traces these steps, showing the variable c changing from undefined to Blue(128). Key moments explain why some variants hold data and how to assign and use them. The visual quiz tests understanding of the variable state and enum definition steps. The snapshot summarizes how to define and use enum variants in Rust.

Practice

(1/5)
1. What is an enum variant in Rust?
easy
A. A function inside a struct
B. A named option inside an enum representing a possible value
C. A variable declared with let
D. A type of loop in Rust

Solution

  1. Step 1: Understand enum structure

    An enum groups related named options called variants.
  2. Step 2: Define variant role

    Each variant represents a possible value the enum can hold.
  3. Final Answer:

    A named option inside an enum representing a possible value -> Option B
  4. Quick Check:

    Enum variant = named option [OK]
Hint: Enum variants are named choices inside enums [OK]
Common Mistakes:
  • Confusing variants with functions
  • Thinking variants are variables
  • Mixing variants with loops
2. Which of the following is the correct syntax to define an enum variant named Red inside an enum Color?
easy
A. enum Color { Red }
B. enum Color = Red;
C. struct Color { Red }
D. let Color = Red;

Solution

  1. Step 1: Recall enum syntax

    Enums use the syntax enum Name { Variant1, Variant2 }.
  2. Step 2: Match correct syntax

    enum Color { Red } correctly defines enum Color with variant Red.
  3. Final Answer:

    enum Color { Red } -> Option A
  4. Quick Check:

    Enum variant syntax = enum Name { Variant } [OK]
Hint: Enum variants go inside braces after enum name [OK]
Common Mistakes:
  • Using '=' instead of braces
  • Defining variants inside struct
  • Using let instead of enum
3. What will be the output of this Rust code?
enum Direction {
    North,
    East,
    South,
    West,
}

fn main() {
    let dir = Direction::East;
    match dir {
        Direction::North => println!("Going North"),
        Direction::East => println!("Going East"),
        Direction::South => println!("Going South"),
        Direction::West => println!("Going West"),
    }
}
medium
A. Going North
B. Going South
C. Going East
D. Going West

Solution

  1. Step 1: Identify enum variant assigned

    The variable dir is set to Direction::East.
  2. Step 2: Match statement output

    The match arm for Direction::East prints "Going East".
  3. Final Answer:

    Going East -> Option C
  4. Quick Check:

    Match on Direction::East prints "Going East" [OK]
Hint: Match prints message for assigned enum variant [OK]
Common Mistakes:
  • Confusing variant names
  • Ignoring match arms
  • Assuming default output
4. Identify the error in this enum variant usage:
enum Status {
    Active,
    Inactive,
}

fn main() {
    let s = Status::active;
}
medium
A. Enum variants must have data inside parentheses
B. Missing semicolon after enum definition
C. Cannot assign enum variant to variable
D. Enum variant names are case-sensitive; should be 'Active'

Solution

  1. Step 1: Check variant usage

    The code uses Status::active but enum defines Active with uppercase A.
  2. Step 2: Understand Rust case sensitivity

    Rust enum variants are case-sensitive; active is undefined.
  3. Final Answer:

    Enum variant names are case-sensitive; should be 'Active' -> Option D
  4. Quick Check:

    Variant names must match case exactly [OK]
Hint: Enum variant names are case-sensitive [OK]
Common Mistakes:
  • Using wrong case for variants
  • Forgetting semicolons (not error here)
  • Thinking variants need data always
5. Given this enum with data:
enum Message {
    Quit,
    Move { x: i32, y: i32 },
    Write(String),
}

fn main() {
    let msg = Message::Move { x: 10, y: 20 };
    match msg {
        Message::Quit => println!("Quit message"),
        Message::Move { x, y } => println!("Move to ({}, {})", x, y),
        Message::Write(text) => println!("Write: {}", text),
    }
}

What will this program print?
hard
A. Move to (10, 20)
B. Quit message
C. Write: Move to (10, 20)
D. Compilation error due to missing variant

Solution

  1. Step 1: Identify enum variant with data

    The variable msg is Message::Move { x: 10, y: 20 }.
  2. Step 2: Match arms handle each variant

    The match arm for Message::Move { x, y } prints coordinates.
  3. Final Answer:

    Move to (10, 20) -> Option A
  4. Quick Check:

    Match prints coordinates for Move variant [OK]
Hint: Match extracts data from enum variant fields [OK]
Common Mistakes:
  • Confusing variant names
  • Ignoring data inside variants
  • Expecting compilation error incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard