TryBugHunt
rustOtherEasy

Reversing an accented word garbles the letters

BugFind the bug and fix the code

What it should do

reverse_text(text) returns the text with its letters in reverse order. Example: reverse_text("héllo") → "olléh"

What happens instead

reverse_text("héllo") returns "oll©Ãh" instead of "olléh".

fn reverse_text(text: &str) -> String {
    text.bytes().rev().map(|b| b as char).collect()
}

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: bytes() walks over the raw UTF-8 bytes. é is stored as two bytes, 0xC3 and 0xA9. Reversing the bytes swaps those two, and b as char turns each single byte into its own character, © and Ã. Plain ASCII text works because each letter is one byte.

The fix: Reverse characters, not bytes: text.chars().rev().collect().

Remember: work with chars() whenever you care about letters. bytes() is for raw data, and turning a byte into a char with as only works for ASCII.

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/86f04f88-860d-4587-9660-66e1368871bb" title="Debugging challenge: Reversing an accented word garbles the letters" 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/86f04f88-860d-4587-9660-66e1368871bb">TryBugHunt</a></p>