Complete the code to check if a string is the same forwards and backwards.
fn is_palindrome(s: &str) -> bool {
s == s.chars().rev().[1]::<String>()
}The reversed characters are collected into a new String using collect:: to compare with the original.
Complete the code to ignore case when checking palindrome.
fn is_palindrome_ignore_case(s: &str) -> bool {
let lower = s.to_lowercase();
lower == lower.chars().rev().[1]::<String>()
}After reversing, we convert the characters back to a String with collect:: to compare ignoring case.
Fix the error in the code to correctly check palindrome ignoring spaces.
fn is_palindrome_ignore_spaces(s: &str) -> bool {
let filtered: String = s.chars().filter(|c| !c.is_whitespace()).collect();
filtered == filtered.chars().rev().[1]::<String>()
}We must convert the reversed characters into a String using collect:: for correct comparison.
Fill both blanks to create a palindrome check ignoring punctuation and case.
fn is_palindrome_clean(s: &str) -> bool {
let filtered: String = s.chars().filter(|c| c.is_alphanumeric()).map(|c| c.to_lowercase().[1]().unwrap()).collect();
filtered == filtered.chars().rev().[2]::<String>()
}Each character's lowercase iterator returns an iterator, so next() gets the char. Then reversed chars are collected into a String with collect::.
Fill both blanks to create a palindrome check ignoring case, spaces, and punctuation.
fn is_palindrome_full(s: &str) -> bool {
let filtered: String = s.chars()
.filter(|c| c.is_alphanumeric())
.map(|c| c.to_lowercase().[1]().unwrap())
.collect();
filtered == filtered.chars().rev().[2]::<String>()
}Use next() to get the lowercase char, then collect reversed chars into a String with collect::.
