TryBugHunt
rustLogic errorEasy

Ten percent off takes ten cents off

BugFind the bug and fix the code

What it should do

apply_discount(price, percent) returns the price after taking percent off it. Example: apply_discount(200.0, 10.0) → 180.0

What happens instead

apply_discount(200.0, 10.0) returns 199.9 instead of 180.0.

fn apply_discount(price: f64, percent: f64) -> f64 {
    price - percent / 100.0
}

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 logic errors pattern

The wrong operator, an inverted condition, a formula the right way round but backwards.

What you'll practise

An easy Rust debugging exercise about logic errors: the wrong operator, an inverted condition, a formula the right way round but backwards.

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: percent / 100.0 turns 10 into 0.1, which is the fraction of the price to remove. But the code subtracts that fraction itself, 0.1, instead of 0.1 of the price. The discount never depends on the price.

The fix: Multiply by the price: price - price * percent / 100.0.

Remember: a percentage is always a percentage of something. Check the units: subtracting 0.1 from 200 mixes a fraction with money. A quick test with round numbers like 200 and 10 shows it at once.

The pattern behind it: logic errors.

More logic errors to practise

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/cb9cf706-da15-440b-87a9-da9329325092" title="Debugging challenge: Ten percent off takes ten cents off" 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/cb9cf706-da15-440b-87a9-da9329325092">TryBugHunt</a></p>