Removing unneeded String creation

This commit is contained in:
2020-11-25 17:51:37 +00:00
parent a69930aa63
commit 87c67f9c3a

View File

@@ -1,16 +1,14 @@
pub fn pig_latin(string: &str) -> String {
// NOTE: does this need to be a String or can we use the &str passed in?
let s = String::from(string);
let mut portion = String::new();
let mut latinised = String::new();
let mut starts_with_vowel = false;
let vowels = ['a', 'e', 'i', 'o', 'u'];
if s.is_empty() {
return s;
if string.is_empty() {
return latinised;
}
let first_character = s.chars().next().unwrap();
let first_character = string.chars().next().unwrap();
if vowels.contains(&first_character) {
portion.push_str("-hay");
@@ -18,7 +16,7 @@ pub fn pig_latin(string: &str) -> String {
} else {
portion = format!("-{}ay", &first_character);
}
for (pos, char) in s.char_indices() {
for (pos, char) in string.char_indices() {
if pos == 0 && !starts_with_vowel {
continue;
}