I just started to learn rust few month ago.
i need help with this fn that i created, need to return palindrome word from the input.
my question,
- is my code right? because when i run it, it just stuck.
- is there better way to make this much simple than it is right now ?
fn main() {
let input = "babad";
println!("{:?}", palindrome_substring(input));
}
fn palindrome_substring(s: &str) -> Vec {
let mut s_vec: Vec<_> = s.chars().collect();
let mut result = Vec::new();
let first_index = 0;
let last_index = s_vec.len() - 1;
while first_index < last_index {
if s_vec[first_index] != s_vec[first_index + 1] && s_vec[first_index] == s_vec[first_index + 2] {
result.push(s_vec[first_index]);
result.push(s_vec[first_index + 1]);
result.push(s_vec[first_index + 2]);
} else if s_vec[first_index] == s_vec[first_index + 1] {
result.push(s_vec[first_index]);
result.push(s_vec[first_index + 1]);
} else {
println!("There is no palindrome subsring in the Input")
}
}
result
}