Bird
Raised Fist0
Rustprogramming~20 mins

Enum variants in Rust - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Enum Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of matching enum variants
What is the output of this Rust program?
Rust
enum Direction {
    North,
    East,
    South,
    West,
}

fn main() {
    let dir = Direction::South;
    match dir {
        Direction::North => println!("Going North"),
        Direction::East => println!("Going East"),
        Direction::South => println!("Going South"),
        Direction::West => println!("Going West"),
    }
}
AGoing South
BGoing North
CGoing West
DGoing East
Attempts:
2 left
💡 Hint
Look at which variant is assigned to dir and which arm matches it.
❓ Predict Output
intermediate
2:00remaining
Output with enum variant holding data
What will this Rust program print?
Rust
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 message: {}", text),
    }
}
AWrite message: Move to (10, 20)
BQuit message
CMove to (10, 20)
DMove to (x, y)
Attempts:
2 left
💡 Hint
Check the variant used and how the data inside it is accessed.
❓ Predict Output
advanced
2:00remaining
Output of enum with tuple variants
What is the output of this Rust code?
Rust
enum Shape {
    Circle(f64),
    Rectangle(f64, f64),
}

fn area(shape: Shape) -> f64 {
    match shape {
        Shape::Circle(radius) => 3.14 * radius * radius,
        Shape::Rectangle(width, height) => width * height,
    }
}

fn main() {
    let c = Shape::Circle(2.0);
    let r = Shape::Rectangle(3.0, 4.0);
    println!("Circle area: {:.2}", area(c));
    println!("Rectangle area: {:.2}", area(r));
}
A
Circle area: 6.28
Rectangle area: 12.00
B
Circle area: 6.28
Rectangle area: 7.00
C
Circle area: 12.56
Rectangle area: 7.00
D
Circle area: 12.56
Rectangle area: 12.00
Attempts:
2 left
💡 Hint
Calculate area of circle with radius 2 and rectangle 3x4.
❓ Predict Output
advanced
2:00remaining
Output when matching nested enum variants
What does this Rust program print?
Rust
enum Status {
    Success,
    Error(String),
}

enum Response {
    Ok(Status),
    Fail(Status),
}

fn main() {
    let res = Response::Fail(Status::Error(String::from("Timeout")));
    match res {
        Response::Ok(Status::Success) => println!("Operation succeeded"),
        Response::Ok(Status::Error(msg)) => println!("Operation failed: {}", msg),
        Response::Fail(Status::Success) => println!("Failure but success status"),
        Response::Fail(Status::Error(msg)) => println!("Failure with error: {}", msg),
    }
}
AFailure but success status
BFailure with error: Timeout
COperation failed: Timeout
DOperation succeeded
Attempts:
2 left
💡 Hint
Look at the outer and inner enum variants used in res.
🧠 Conceptual
expert
2:00remaining
Number of variants in this enum
How many distinct variants does this Rust enum have?
Rust
enum Event {
    Click,
    KeyPress(char),
    Resize { width: u32, height: u32 },
    Hover,
    Scroll(i32),
}
A5
B4
C3
D6
Attempts:
2 left
💡 Hint
Count each variant name regardless of data it holds.

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