Bird
Raised Fist0
Rustprogramming~20 mins

Match with enums in Rust - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Rust Match with Enums Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of matching enum variants with data
What is the output of this Rust program?
Rust
enum Status {
    Success(u8),
    Error(String),
    Pending,
}

fn main() {
    let s = Status::Success(200);
    match s {
        Status::Success(code) => println!("Code: {}", code),
        Status::Error(msg) => println!("Error: {}", msg),
        Status::Pending => println!("Pending..."),
    }
}
ACode: Success(200)
BError: 200
CPending...
DCode: 200
Attempts:
2 left
💡 Hint
Look at how the match extracts the inner value from the Success variant.
❓ Predict Output
intermediate
2:00remaining
Matching nested enums with multiple patterns
What will this Rust program print?
Rust
enum Direction {
    Left,
    Right,
}

enum Command {
    Move(Direction),
    Stop,
}

fn main() {
    let cmd = Command::Move(Direction::Right);
    match cmd {
        Command::Move(Direction::Left) => println!("Moving left"),
        Command::Move(Direction::Right) => println!("Moving right"),
        Command::Stop => println!("Stopped"),
    }
}
AMoving right
BMoving left
CStopped
DCompile error
Attempts:
2 left
💡 Hint
Check which Direction variant is inside the Command::Move variant.
🔧 Debug
advanced
2:00remaining
Identify the error in matching enum with missing variant
What error will this Rust code produce when compiled?
Rust
enum Light {
    Red,
    Yellow,
    Green,
}

fn main() {
    let light = Light::Yellow;
    match light {
        Light::Red => println!("Stop"),
        Light::Green => println!("Go"),
    }
}
APrints nothing
BRuntime panic: no matching pattern found
CCompile error: non-exhaustive patterns in match
DCompile error: unused variable 'light'
Attempts:
2 left
💡 Hint
Rust requires all enum variants to be matched or covered by a wildcard.
📝 Syntax
advanced
2:00remaining
Correct syntax for matching enum with data
Which option correctly matches and extracts data from this enum variant?
Rust
enum Message {
    Text(String),
    Quit,
}

fn main() {
    let msg = Message::Text(String::from("Hello"));
    match msg {
        // Fill in the correct pattern here
    }
}
AMessage::Text(text) => println!(text),
BMessage::Text(text) => println!("Text: {}", text),
CMessage::Text(text) { println!("Text: {}", text); },
DMessage::Text => println!("Text: {}", text),
Attempts:
2 left
💡 Hint
The pattern must bind the inner String to a variable and use it in println! macro.
🚀 Application
expert
3:00remaining
Determine the final value after matching and updating enum
Given this Rust code, what is the final value of 'status' after the match block?
Rust
enum Status {
    Active(i32),
    Inactive,
}

fn main() {
    let mut status = Status::Active(10);
    match &mut status {
        Status::Active(ref mut val) if *val > 5 => *val += 5,
        Status::Active(_) => (),
        Status::Inactive => status = Status::Active(1),
    }
    // What is the value of status here?
}
AStatus::Active(15)
BStatus::Active(10)
CStatus::Active(1)
DStatus::Inactive
Attempts:
2 left
💡 Hint
The match uses a guard to check if the value is greater than 5 and updates it.

Practice

(1/5)
1.

What does the match statement do when used with enums in Rust?

easy
A. It checks the enum variant and runs the matching code block.
B. It converts the enum into a string automatically.
C. It creates a new enum variant.
D. It deletes the enum variant from memory.

Solution

  1. Step 1: Understand the purpose of match with enums

    The match statement compares the enum value against each variant.
  2. Step 2: Identify what happens on a match

    When a variant matches, the corresponding code block runs.
  3. Final Answer:

    It checks the enum variant and runs the matching code block. -> Option A
  4. Quick Check:

    Match runs code by enum variant [OK]
Hint: Match arms correspond to enum variants directly [OK]
Common Mistakes:
  • Thinking match converts enums to strings
  • Assuming match creates or deletes variants
  • Confusing match with if-else statements
2.

Which of the following is the correct syntax to match an enum Color with variants Red, Green, and Blue?

enum Color { Red, Green, Blue }
easy
A. match color { Color.Red => {}, Color.Green => {}, Color.Blue => {} }
B. match color { Red => {}, Green => {}, Blue => {} }
C. match color { Color::Red => {}, Color::Green => {}, Color::Blue => {} }
D. match color { 'Red' => {}, 'Green' => {}, 'Blue' => {} }

Solution

  1. Step 1: Recall enum variant syntax in match

    Enum variants must be prefixed with the enum name and double colon, like Color::Red.
  2. Step 2: Check each option's syntax

    Only match color { Color::Red => {}, Color::Green => {}, Color::Blue => {} } uses the correct Color::Variant syntax.
  3. Final Answer:

    match color { Color::Red => {}, Color::Green => {}, Color::Blue => {} } -> Option C
  4. Quick Check:

    Enum variants use EnumName::Variant [OK]
Hint: Use EnumName::Variant in match arms [OK]
Common Mistakes:
  • Omitting enum name prefix
  • Using dot instead of double colon
  • Using strings instead of variants
3.

What will be the output of this Rust code?

enum Direction { North, South, East, West }

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

Solution

  1. Step 1: Identify the enum variant assigned

    The variable dir is set to Direction::East.
  2. Step 2: Match dir to the correct message

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

    Going right -> Option A
  4. Quick Check:

    Direction::East matches "Going right" [OK]
Hint: Match returns value for the exact enum variant [OK]
Common Mistakes:
  • Mixing up enum variants and their messages
  • Forgetting to use EnumName::Variant in match
  • Assuming default or fallback arm runs
4.

Find the error in this Rust code snippet using match with enums:

enum Status { Success, Failure }

fn check(status: Status) {
    match status {
        Status::Success => println!("Success"),
        // Missing Failure arm
    }
}
medium
A. Missing semicolon after println! macro
B. Function check should return a value
C. Incorrect enum variant syntax
D. Missing match arm for Status::Failure

Solution

  1. Step 1: Check completeness of match arms

    The match statement covers Status::Success but misses Status::Failure.
  2. Step 2: Understand Rust's exhaustiveness requirement

    Rust requires all enum variants to be matched or a catch-all arm (_) to avoid compile errors.
  3. Final Answer:

    Missing match arm for Status::Failure -> Option D
  4. Quick Check:

    All enum variants must be matched [OK]
Hint: Match must cover all enum variants or use _ arm [OK]
Common Mistakes:
  • Leaving out enum variants in match arms
  • Assuming partial match compiles
  • Confusing semicolon placement with match completeness
5.

Given this enum with data:

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

Which match arm correctly extracts the x and y values from Message::Move?

hard
A. Message::Move(x, y) => println!("x: {}, y: {}", x, y),
B. Message::Move { x, y } => println!("x: {}, y: {}", x, y),
C. Message::Move(x: x, y: y) => println!("x: {}, y: {}", x, y),
D. Message::Move(x: x, y) => println!("x: {}, y: {}", x, y),

Solution

  1. Step 1: Identify the enum variant with named fields

    Move uses named fields { x: i32, y: i32 }.
  2. Step 2: Match syntax for named fields

    Use Message::Move { x, y } to destructure named fields directly.
  3. Final Answer:

    Message::Move { x, y } => println!("x: {}, y: {}", x, y), -> Option B
  4. Quick Check:

    Named fields use braces and field names [OK]
Hint: Use braces and field names to destructure named fields [OK]
Common Mistakes:
  • Using tuple pattern for struct-like variant
  • Mixing colon syntax incorrectly
  • Omitting braces for named fields
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