From 87c67f9c3ac3a1a0d6240adedac8884740d9c65f Mon Sep 17 00:00:00 2001 From: Daniel Tomlinson Date: Wed, 25 Nov 2020 17:51:37 +0000 Subject: [PATCH] Removing unneeded String creation --- src/pig_latin.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/pig_latin.rs b/src/pig_latin.rs index 66ca179..36f10d8 100644 --- a/src/pig_latin.rs +++ b/src/pig_latin.rs @@ -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; }