TryBugHunt
rustOtherEasy

Name José is counted as five letters

BugFind the bug and fix the code

What it should do

name_length(name) returns how many letters a name has, so a form can limit name length. Example: name_length("José") → 4

What happens instead

name_length("José") returns 5 instead of 4.

fn name_length(name: &str) -> usize {
    name.len()
}

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: A Rust String is stored as UTF-8 bytes, and len() returns the number of bytes, not letters. Plain English letters use one byte each, but é uses two. So "José" is 4 letters and 5 bytes.

The fix: Count characters with name.chars().count().

Remember: len() on a string is a byte count. It matches the letter count only for plain ASCII text. Any time the text could hold accents, other alphabets or emoji, count with chars().

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/00a8049c-8123-4e4e-927a-0cdb80cd99e7" title="Debugging challenge: Name José is counted as five 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/00a8049c-8123-4e4e-927a-0cdb80cd99e7">TryBugHunt</a></p>