diff --git a/src/pig_latin.rs b/src/pig_latin.rs index d194ca0..583d41f 100644 --- a/src/pig_latin.rs +++ b/src/pig_latin.rs @@ -7,6 +7,7 @@ pub fn showroom() { let uppercase = String::from("UPPERCASE"); let accent = String::from("école"); let accent_multiples_char = String::from("église"); + let empty = String::from(""); let consonant = to_pig_latin(consonant); println!("And the suffix to add is: {}", consonant); @@ -20,6 +21,8 @@ pub fn showroom() { println!("And the suffix to add is: {}", accent); let accent_multiples_char = to_pig_latin(accent_multiples_char); println!("And the suffix to add is: {}", accent_multiples_char); + let empty = to_pig_latin(empty); + println!("And the suffix to add is: {}", empty); println!(); } @@ -29,16 +32,24 @@ pub fn to_pig_latin(mut s: String) -> String { // FIXME: is there something better than removing the 1st char like this ? // Since 2 of the 3 branches need to reform the word. - let first_letter = s.remove(0); + let first_letter = s.chars().next(); + + let first_letter = if let Some(first) = first_letter { + first + } else { + println!("Your string is empty, impossible to transform this string to pig latin."); + return s; + }; if !first_letter.is_alphabetic() { - // Not starting with a letter, ignore and reset the letter - format!("{}{}", first_letter, s) + println!("Not starting with a letter, impossible to transform this string to pig latin."); + s } else if vowels.contains(&first_letter.to_ascii_lowercase()) { // Starting with a vowel, reform the word and place add "-hay" - format!("{}{}-hay", first_letter, s) + format!("{}-hay", s) } else { // Starting with a consonant, place the consonant at the end of the word followed by "ay" + s.remove(0); format!("{}-{}ay", s, first_letter) } }