diff options
Diffstat (limited to 'rust/acronym/src/lib.rs')
| -rw-r--r-- | rust/acronym/src/lib.rs | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/rust/acronym/src/lib.rs b/rust/acronym/src/lib.rs new file mode 100644 index 0000000..dcd4615 --- /dev/null +++ b/rust/acronym/src/lib.rs | |||
| @@ -0,0 +1,19 @@ | |||
| 1 | fn to_acronym(word: &str) -> Option<String> { | ||
| 2 | if word.chars().all(|c| c.is_uppercase() || !c.is_alphabetic()) | ||
| 3 | || word.chars().all(|c| c.is_lowercase() || !c.is_alphabetic()) | ||
| 4 | { | ||
| 5 | word.chars() | ||
| 6 | .find(|c| c.is_alphabetic()) | ||
| 7 | .map(|c| c.to_uppercase().to_string()) | ||
| 8 | } else { | ||
| 9 | Some(word.chars().filter(|c| c.is_uppercase()).collect()) | ||
| 10 | } | ||
| 11 | } | ||
| 12 | |||
| 13 | pub fn abbreviate(phrase: &str) -> String { | ||
| 14 | phrase | ||
| 15 | .split(|c| " -:,".contains(c)) // Split into words | ||
| 16 | .filter(|s| !s.is_empty()) | ||
| 17 | .flat_map(to_acronym) | ||
| 18 | .collect() | ||
| 19 | } | ||
