The title says it all. I'm reading "The Rust Programming Language" 2nd ed. right now, and just finished the "Collections" chapter. I know there's a ton of Pig Latin code here, but I think Rust deserves a separate question.
use std::io;
use std::iter::FromIterator;
fn main() {
println!("Enter the string to be Pig-latinized");
let mut input = String::new();
io::stdin().read_line(&mut input)
.expect("Failed to read from stdin");
let s: Vec<char> = input.chars().collect();
let mut new_s: Vec<char> = Vec::new();
let consonants = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q',
't', 'r', 's', 'v', 'w', 'x', 'z', 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q',
'T', 'R', 'S', 'V', 'W', 'X', 'Z'];
let mut i = 0;
while i < s.len() {
if !s[i].is_alphabetic() {
new_s.push(s[i]);
i += 1;
continue;
}
let first_letter = s[i];
let mut appendage: Vec<char> = "-hay".chars().collect();
if consonants.contains(&first_letter) {
appendage[1] = first_letter;
i += 1;
}
while s[i].is_alphabetic() && i < s.len() {
new_s.push(s[i]);
i += 1;
continue;
}
new_s.append(&mut appendage);
}
println!("{}", String::from_iter(new_s));
}
The code feels clunky, but I don't really know how to make it better. I'd love to see an idiomatic Rust solution!