TryBugHunt
rustOtherEasy

Prices show one zero instead of two

BugFind the bug and fix the code

What it should do

format_price(cents) turns an amount in cents into a dollar string with exactly two digits after the point. Example: format_price(1205) → "$12.05"

What happens instead

format_price(1205) returns "$12.5" instead of "$12.05", and format_price(500) returns "$5.0".

fn format_price(cents: i32) -> String {
    format!("${}.{}", cents / 100, cents % 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 mutation and copying bugs pattern

Cloning where a borrow was meant, or changing one value while expecting another to follow.

What you'll practise

An easy Rust debugging exercise about mutation and copying bugs: cloning where a borrow was meant, or changing one value while expecting another to follow.

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: cents % 100 is the number 5 for 1205, and {} prints a number with no padding, so it prints 5, not 05. The result reads as 12 dollars and 50 cents, which is wrong.

The fix: Pad the cents to two digits with zeros: {:02}. Now 5 prints as 05 and 0 prints as 00.

Remember: numbers lose their leading zeros when printed. Wherever a fixed width matters (cents, minutes, seconds, months), use a width and fill like {:02} in the format string.

The pattern behind it: mutation and copying bugs.

More mutation and copying bugs 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/e23550e2-118f-4821-8e60-6af5092c8a9f" title="Debugging challenge: Prices show one zero instead of two" 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/e23550e2-118f-4821-8e60-6af5092c8a9f">TryBugHunt</a></p>