0
0
Rustprogramming~10 mins

Generic enums in Rust - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Generic enums
Define generic enum with type parameter T
Create enum instance with specific type
Match on enum variant
Variant A
Use value of type T
End of match
This flow shows defining a generic enum, creating instances with specific types, and matching on variants to use the contained values.
Execution Sample
Rust
enum Option<T> {
    Some(T),
    None,
}

fn main() {
    let x: Option<i32> = Option::Some(5);
    match x {
        Option::Some(val) => println!("Value: {}", val),
        Option::None => println!("No value"),
    }
}
Defines a generic enum Option with type T, creates an instance with i32, and matches to print the value.
Execution Table
StepActionEvaluationResult
1Define enum Option<T>Generic enum with variants Some(T) and NoneEnum ready for use
2Create x = Option::Some(5)x is Option<i32> with Some(5)x holds Some(5)
3Match on xx is Some(5)Go to Some branch
4Print valueval = 5Output: Value: 5
5End matchNo more variantsMatch complete
💡 Match ends after handling Some variant with value 5
Variable Tracker
VariableStartAfter Step 2After Step 3Final
xuninitializedSome(5)Some(5)Some(5)
valN/AN/A55
Key Moments - 3 Insights
Why do we write Option<T> instead of just Option?
Option<T> means the enum can hold any type T. This is shown in step 1 of the execution_table where the enum is defined with a generic type parameter T.
How does Rust know what type T is when we create x?
In step 2, we create x as Option<i32> by specifying the type. This tells Rust that T is i32 for this instance.
What happens if x is Option::None?
In the match (step 3), if x was None, the code would go to the None branch and print 'No value' instead of 'Value: 5'.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of x after step 2?
ASome(5)
BNone
CUninitialized
D5
💡 Hint
Check the 'After Step 2' column for variable x in variable_tracker.
At which step does the program print the output?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look at the 'Action' and 'Result' columns in execution_table for printing.
If we changed x to Option::None, which step's result would change?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Consider the match branches in step 3 and what is printed in step 4.
Concept Snapshot
Generic enums allow defining enums with a type parameter T.
You create instances by specifying the concrete type, e.g., Option<i32>.
Match on enum variants to access the contained value of type T.
This enables flexible, reusable enum definitions.
Full Transcript
This example shows how to define a generic enum Option with a type parameter T. We create an instance x of type Option<i32> holding the value Some(5). Then we match on x to check which variant it is. Since x is Some(5), we print the value 5. The execution table traces each step: defining the enum, creating x, matching on x, printing the value, and ending the match. The variable tracker shows how x and val change during execution. Key moments clarify why we use generics, how Rust infers types, and what happens if the enum is None. The quiz tests understanding of variable values and match behavior. This helps beginners see how generic enums work step-by-step in Rust.