TryBugHunt
rustType errorEasy

Price with cents is read as zero

BugFind the bug and fix the code

What it should do

parse_price(input) reads a price typed by a user, such as "3.50", and returns 0 if it is not a number. Example: parse_price("3.50") → 3.5

What happens instead

parse_price("3.50") returns 0.0 instead of 3.5.

fn parse_price(input: &str) -> f64 {
    input.trim().parse::<i32>().unwrap_or(0) as f64
}

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: parse::<i32>() reads a whole number. "3.50" has a decimal point, which is not valid in an integer, so parsing fails. unwrap_or(0) then hides the error and the price becomes 0. Whole prices like "12" still work, which makes the bug easy to miss.

The fix: Parse the type you actually need: parse::<f64>().unwrap_or(0.0).

Remember: the type in parse::<T>() decides what text is accepted. A fallback like unwrap_or hides parse errors, so test inputs that should succeed, not only ones that should fail.

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/cf9e6a79-3e4d-4236-8686-739798858f51" title="Debugging challenge: Price with cents is read as zero" 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/cf9e6a79-3e4d-4236-8686-739798858f51">TryBugHunt</a></p>