TryBugHunt
rustOtherMedium

Dashes stay in the cleaned phone number

BugFind the bug and fix the code

What it should do

clean_phone(phone) removes dashes and spaces from a phone number so only digits are left. Example: clean_phone("555-123 4567") → "5551234567"

What happens instead

clean_phone("555-123 4567") returns "555-1234567": the space is gone but the dash is still there.

fn clean_phone(phone: &str) -> String {
    let digits = phone.trim().to_string();
    digits.replace("-", "");
    digits.replace(" ", "")
}

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

A medium 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: replace() does not change the string it is called on. It returns a new String with the changes. The result of the first replace is never stored, so it is thrown away, and the second replace works on the original text that still has its dashes.

The fix: Keep the result: let digits = digits.replace("-", "");. You could also chain both calls on one line.

Remember: most string methods in Rust (replace, trim, to_lowercase) return a new value. If you call one without using what it returns, nothing happens. The compiler warns about this for many of them.

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/e668baad-4e14-4c36-b7bb-c34819b45360" title="Debugging challenge: Dashes stay in the cleaned phone number" 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/e668baad-4e14-4c36-b7bb-c34819b45360">TryBugHunt</a></p>