I just finished the chapter about collections from the Rust book, and it recommended trying some exercises provided there, such as a Pig Latin translator.
Convert strings to pig latin. The first consonant of each word is moved to the end of the word and “ay” is added, so “first” becomes “irst-fay.” Words that start with a vowel have “hay” added to the end instead (“apple” becomes “apple-hay”). Keep in mind the details about UTF-8 encoding!
use std::io;
fn main() {
let mut inp = String::new();
io::stdin().read_line(&mut inp).expect("Could not read");
inp.remove(inp.len() - 1); // newline
let cha = inp.chars().next().expect("Empty string");
if is_consonant(cha) {
inp += "-hay";
} else {
inp.remove(0);
inp += &format!("-{}ay", cha);
}
println!("{}", inp);
}
fn is_consonant(c: char) -> bool {
['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'].contains(&c)
}
Does my code satisfy whatever is required for it to be idiomatic Rust?
is_consonant()
has a very misleading name - that sort of thing will certainly trip you up one day. \$\endgroup\$