TryBugHunt
rustScope errorMedium

Variable shadowing in match arm pattern binding

BugFind the bug and fix the code

What it should do

Check if code matches target expected code. Example: match_expected_code(200, 200) -> true

What happens instead

Match arm unconditionally matches because variable name binds a new pattern instead of comparing

fn match_expected_code(actual: i32, expected: i32) -> bool {
    match actual {
        // 'expected' here binds a new local variable shadowing the parameter!
        expected => true,
        _ => false,
    }
}

fn main() {
    println!("{}", match_expected_code(404, 200));
}

Test a custom input to verify an edge case (e.g. empty lists, negative numbers, duplicates).

⏱️0:00
Hints
🏆 0 hints = Flawless Solve
Think & Test Cooldown
2:00 remaining

Take a moment to read the bug symptom and trace the code with Run & Check before unlocking Hint 1.

Hint 2
Unlocks after Hint 1
Hint 3
Unlocks after Hint 2

This bug belongs to the scope and closure bugs pattern

A shadowed binding, or a value borrowed after the scope that owned it has ended.

What you'll practise

A medium Rust debugging exercise about scope and closure bugs: a shadowed binding, or a value borrowed after the scope that owned it has ended.

The checks your fix must pass

Signs of this kind of bug

Spoiler: why this bug happens(try it first; the hints get warmer one at a time)

What went wrong: In a match arm, an identifier pattern (expected) introduces a new variable that matches anything and shadows the outer parameter. It does not perform an equality comparison.

The fix: Use a match guard: code if code == expected => true.

Remember: Variable names in match patterns create bindings. Use match guards (if x == y) or constants to compare against existing variables.

The pattern behind it: scope and closure bugs.

More scope and closure bugs to practise

Related errors

Embed this challenge on your site

Paste this into any web page or blog post. Visitors fix the bug right there, in their browser.

<iframe src="https://trybughunt.com/embed/07515f6f-de3d-4c33-bca2-be9dd58d1ee1" title="Debugging challenge: Variable shadowing in match arm pattern binding" width="100%" height="580" loading="lazy" style="border:1px solid #d4d4d8;border-radius:12px;max-width:760px"></iframe>
<p style="font-size:14px">Debugging challenge from <a href="https://trybughunt.com/challenges/07515f6f-de3d-4c33-bca2-be9dd58d1ee1">TryBugHunt</a></p>