TryBugHunt
rustType errorEasy

Negative odd numbers reported as even

BugFind the bug and fix the code

What it should do

is_odd(n) returns true if the whole number n is odd, including negative numbers. Example: is_odd(-3) → true

What happens instead

is_odd(-3) returns false instead of true.

fn is_odd(n: i32) -> bool {
    n % 2 == 1
}

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: In Rust, % is a remainder that keeps the sign of the left side. -3 % 2 is -1, not 1. Comparing with == 1 only catches positive odd numbers, so every negative odd number is reported as even.

The fix: Check for a non-zero remainder instead: n % 2 != 0. That works for both signs.

Remember: % on negative integers gives a negative or zero result in Rust (and C, Java, Go, JavaScript). Compare with 0, or use rem_euclid(2) when you need a result that is never negative.

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/0386c0c9-c0cb-4806-994f-49675c6f4ecc" title="Debugging challenge: Negative odd numbers reported as even" 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/0386c0c9-c0cb-4806-994f-49675c6f4ecc">TryBugHunt</a></p>