From 06ce6d760b0d207c1bef9581432fc14aa3ca6a0c Mon Sep 17 00:00:00 2001 From: Jim Infield Date: Sun, 4 Jul 2021 14:00:37 -0500 Subject: [PATCH] Refactor to utilize iterators and matching, rather than if --- src/lib.rs | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ebb9bbe..577672e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,28 +1,12 @@ pub fn reply(message: &str) -> &str { - let mut question = false; - if message.trim().chars().last() == Some('?') { - question = true; - } - - let mut u = 0; - let mut l = 0; - let mut n = 0; - let mut shouted = false; - let mut silence = false; - - for char in message.chars() { - if char.is_uppercase() { - u += 1; - } else if char.is_lowercase() { - l += 1; - } else if char.is_numeric() { - n += 1; - } - } + let uc = message.matches(char::is_uppercase).count(); + let lc = message.matches(char::is_lowercase).count(); + let an = message.matches(char::is_alphanumeric).count(); - if u > l { shouted = true; } - if u+l+n == 0 { silence = true; } + let question = message.trim().chars().last() == Some('?'); + let shouted = uc > lc; + let silence = an == 0; match (question,shouted, silence) { (true,true, _ ) => "Calm down, I know what I'm doing!",