TryBugHunt
rustType errorMedium

Negative brightness turns into a bright color

BugFind the bug and fix the code

What it should do

to_color_channel(value) limits a color value to the range 0 to 255 and returns it as a byte. Values above 255 become 255 and values below 0 become 0. Example: to_color_channel(300) → 255

What happens instead

to_color_channel(-10) returns 246 instead of 0.

fn to_color_channel(value: i32) -> u8 {
    value.min(255) as u8
}

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

A medium 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: min(255) handles values that are too big, but nothing handles values below 0. Casting a negative i32 with as u8 does not fail or clamp. It keeps only the lowest 8 bits, so -10 wraps around to 246, an almost full brightness.

The fix: Clamp both ends before the cast: value.max(0).min(255) as u8.

Remember: as between integer types never checks the range. It silently wraps. Clamp the value first, or use u8::try_from to get an error you can handle.

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/4b252c56-4f9b-4717-a68f-402a48ec1326" title="Debugging challenge: Negative brightness turns into a bright color" 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/4b252c56-4f9b-4717-a68f-402a48ec1326">TryBugHunt</a></p>