TryBugHunt
rustScope errorEasy

Member discount never shows up in the total

BugFind the bug and fix the code

What it should do

member_price(price, is_member) returns the price, with 10% off for members. Example: member_price(100.0, true) → 90.0

What happens instead

member_price(100.0, true) returns 100.0 instead of 90.0.

fn member_price(price: f64, is_member: bool) -> f64 {
    let total = price;
    if is_member {
        let total = price * 0.9;
    }
    total
}

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

An easy 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: let inside the if block creates a brand new variable that happens to have the same name. This is called shadowing. The new total exists only inside the braces and disappears at the closing }. The outer total is never changed, so members pay full price.

The fix: Make the outer variable mutable and assign to it without let: let mut total = price; then total = price * 0.9;.

Remember: let always makes a new variable. To change an existing one, declare it with mut and assign without let. The compiler's "unused variable" warning is a strong clue here.

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/632be3e2-f0b0-448f-8f22-c0753327f3ad" title="Debugging challenge: Member discount never shows up in the total" 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/632be3e2-f0b0-448f-8f22-c0753327f3ad">TryBugHunt</a></p>