TryBugHunt
rustType errorEasy

Progress shows 0 percent until the task is done

BugFind the bug and fix the code

What it should do

percent(part, whole) returns how many percent part is of whole, rounded down. Example: percent(3, 4) → 75

What happens instead

percent(3, 4) returns 0 instead of 75.

fn percent(part: i32, whole: i32) -> i32 {
    part / whole * 100
}

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

Integer overflow, truncating casts with as, and types that do not convert the way you expect.

What you'll practise

An easy Rust debugging exercise about type errors: integer overflow, truncating casts with as, and types that do not convert the way you expect.

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: The expression is read left to right. part / whole runs first, and with integers 3 / 4 is 0. Then 0 * 100 is still 0. Every value smaller than whole becomes 0, and only part == whole gives 100.

The fix: Multiply first, then divide: part * 100 / whole. 300 / 4 is 75.

Remember: with integer division, the order of * and / changes the answer. Multiply before you divide so the small fraction is not thrown away.

The pattern behind it: type errors.

More type 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/ccf19ba7-9c81-4921-8474-b88e3c043039" title="Debugging challenge: Progress shows 0 percent until the task is done" 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/ccf19ba7-9c81-4921-8474-b88e3c043039">TryBugHunt</a></p>