TryBugHunt
rustScope errorEasy

Canada orders are charged the overseas rate

BugFind the bug and fix the code

What it should do

shipping_cost(country, items) returns a base fee (5 for US, 8 for CA, 15 elsewhere) plus 2 per item. Example: shipping_cost("CA", 1) → 10

What happens instead

shipping_cost("CA", 1) returns 17 instead of 10, the same as an overseas order.

fn shipping_cost(country: &str, items: i32) -> i32 {
    let mut base = 15;
    match country {
        "US" => base = 5,
        "CA" => {
            let base = 8;
        }
        _ => {}
    }
    base + items * 2
}

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: In the "CA" arm, let base = 8; declares a new variable called base that lives only inside that arm's braces. The outer base is never touched and keeps its default of 15. The US arm works because it assigns without let.

The fix: Assign to the existing variable: base = 8; with no let.

Remember: let inside braces shadows the outer variable instead of changing it. The compiler warns "unused variable: base" in this case, so read warnings even when the code compiles.

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/46000001-44ad-418d-afe9-6dfac1803d2c" title="Debugging challenge: Canada orders are charged the overseas rate" 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/46000001-44ad-418d-afe9-6dfac1803d2c">TryBugHunt</a></p>