Browse Source

Refactor to utilize iterators and matching, rather than if

master
Jim Infield 3 years ago
parent
commit
06ce6d760b
  1. 28
      src/lib.rs

28
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!",

Loading…
Cancel
Save