Removing unneeded String creation
This commit is contained in:
@@ -1,16 +1,14 @@
|
|||||||
pub fn pig_latin(string: &str) -> String {
|
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 portion = String::new();
|
||||||
let mut latinised = String::new();
|
let mut latinised = String::new();
|
||||||
let mut starts_with_vowel = false;
|
let mut starts_with_vowel = false;
|
||||||
let vowels = ['a', 'e', 'i', 'o', 'u'];
|
let vowels = ['a', 'e', 'i', 'o', 'u'];
|
||||||
|
|
||||||
if s.is_empty() {
|
if string.is_empty() {
|
||||||
return s;
|
return latinised;
|
||||||
}
|
}
|
||||||
|
|
||||||
let first_character = s.chars().next().unwrap();
|
let first_character = string.chars().next().unwrap();
|
||||||
|
|
||||||
if vowels.contains(&first_character) {
|
if vowels.contains(&first_character) {
|
||||||
portion.push_str("-hay");
|
portion.push_str("-hay");
|
||||||
@@ -18,7 +16,7 @@ pub fn pig_latin(string: &str) -> String {
|
|||||||
} else {
|
} else {
|
||||||
portion = format!("-{}ay", &first_character);
|
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 {
|
if pos == 0 && !starts_with_vowel {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user