Palindrome Check
🎯 In Python, you might check a palindrome like this:
cleaned = "".join(c.lower() for c in s if c.isalnum())
return cleaned == cleaned[::-1]
This exercise is about doing the same thing in Rust — but it's a great chance to practice composing iterator chains, combining the tools you've picked up in the previous exercises.
char methods you'll find useful
Rust's char type has built-in methods that mirror Python's str methods, but they work on individual characters:
'A'.is_alphanumeric() // true (like Python's str.isalnum())
'!'.is_alphanumeric() // false
'A'.to_lowercase() // an iterator yielding 'a'
One subtlety: char::to_lowercase() returns an iterator, not a single char. Why? Because some Unicode characters lowercase into multiple characters (e.g., German ß). This means you can't just .map() it into a single char — you'll need .flat_map() to flatten those inner iterators into one stream:
let letters: String = "Hello!"
.chars()
.flat_map(|c| c.to_lowercase())
.collect();
// "hello!"
.flat_map() is like .map() followed by .flatten() — if your closure returns an iterator (or Option, or Vec), it unpacks the results into a single flat sequence. Python's equivalent is roughly a nested comprehension: [x for group in items for x in group].
The turbofish ::<Type>
Sometimes .collect() can't infer the target type from context. You can specify it inline with turbofish syntax:
let s = some_iterator.collect::<String>();
This is equivalent to let s: String = some_iterator.collect(); — just a different place to put the type annotation.
Login to see the full task and start coding.
This is a premium exercise
Log in to unlock the full exercise and start coding.
Login to access this exercise