HashSet with len

Pangram
Pangram in Rust
use std::collections::HashSet;

pub fn is_pangram(sentence: &str) -> bool {
    sentence
        .to_lowercase()
        .chars()
        .filter(|c| c.is_ascii_alphabetic())
        .collect::<HashSet<char>>()
        .len()
        == 26
}

This approach chains several functions together to determine the result.

  • It first passes the sentence to_lowercase.
  • The lowercased sentence is then iterated by chars.
  • The chars are filtered in its closure so that only a character that is_ascii_alphabetic makes it through to be collected into a HashSet.
  • The function returns if the len of the HashSet is 26. If the number of unique letters in the HashSet is equal to the 26 letters in the alphabet, then the function will return true.
6th Nov 2024 · Found it useful?