This is my implementation of the Pig Latin recommended exercise in The Rust Programming Language book. Any pointers on making this code more idiomatic or run more optimal?
fn pig_latin() {
let s = String::from("Some abobus string 32 text привет абоба 123");
let mut new_s = s.clone();
let consonant = Vec::from([
'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r',
's', 't', 'v', 'w', 'x', 'y', 'z', 'б', 'в', 'г', 'д', 'ж', 'з', 'й',
'к', 'л', 'м', 'н', 'п', 'р', 'с', 'т', 'ф', 'х', 'ц', 'ч', 'ш', 'щ'
]); // Согласные
for word in s.split_whitespace() {
let chars = word.chars();
if let Some(ch) = chars.peekable().peek() {
if ch.is_alphabetic() {
if consonant.contains(ch.to_lowercase().peekable().peek().map_or(ch, |v| v )) {
new_s = new_s.replace(word, &format!("{}-{}ay", &word[ch.len_utf8()..word.len()], ch));
} else {
new_s = new_s.replace(word, &format!("{}-hay", &word[0..word.len()]));
}
}
}
}
println!("Old: '{}' \nNew: '{}'", s, new_s);
}