Skip to content

Instantly share code, notes, and snippets.

@peaceman
Created August 2, 2020 14:18
Show Gist options
  • Save peaceman/b22ca69db6c5b27b66e938ec47b112f2 to your computer and use it in GitHub Desktop.
Save peaceman/b22ca69db6c5b27b66e938ec47b112f2 to your computer and use it in GitHub Desktop.
rust lang book practice collections
fn is_vowel(c: char) -> bool {
let lc = c.to_lowercase().next();
if let None = lc {
return false;
}
let lc = lc.unwrap();
match lc {
'a' | 'e' | 'i' | 'o' | 'u' => true,
_ => false,
}
}
fn pig_latin_convert_text(input: &str) -> String {
input.split_whitespace()
.map(|w| pig_latin_convert_word(w))
.collect::<Vec<String>>()
.join(" ")
}
fn pig_latin_convert_word(word: &str) -> String {
let first_char = word.chars().next();
if let None = first_char {
return word.to_string();
}
let first_char = first_char.unwrap();
let mut word = String::from(word);
if is_vowel(first_char) {
word.push_str("-hay");
} else {
word.replace_range(..first_char.len_utf8(), "");
word.push_str(format!("-{}ay", first_char).as_str());
}
word
}
fn main() {
println!("{}", pig_latin_convert_text("foo the bar arc"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment