Warning
This post was published 42 days ago. The information described in this article may have changed.
Ok, the good news is that it seems to work according to the specs in the book...
Thing is, it looks ugly
use std::io;
fn main() {
loop {
println!("Please enter sentence to pigify:");
let mut input = String::new();
io::stdin()
.read_line(&mut input)
.expect("Failed to read line");
let input = input.trim();
if input.is_empty() {
break;
};
for word in input.split_ascii_whitespace() {
let mut ending = String::new();
let mut pigword = String::new();
for char in word.chars() {
if ending.is_empty() {
match char.to_ascii_uppercase() {
'A' | 'E' | 'I' | 'O' | 'U' => {
ending = "h".to_string().to_owned();
pigword.push_str(char.to_string().as_str())
}
_ => ending = char.to_string().to_owned(),
}
ending.push_str("ay");
} else {
pigword.push_str(char.to_string().as_str());
}
}
pigword.push_str(&ending);
print!("{} ", pigword);
}
println!();
}
}
What bothers me most is the repeated type conversions... I also wanted to use Option to handle the first pass ("ending" being empty) but it got totally out of hand quickly so I went back to more familiar logic. Would an Option make any sense here or is the current IF good enough?
3 posts - 3 participants
🏷️ rust_feed