For example, for
let n = count_unique_grapheme_clusters("π§π· π·πΊ π§π· πΊπΈ π§π·");
println!("{}", n);
the expected output is (space and three flags: " "
, "π§π·"
, "π·πΊ"
, "πΊπΈ"
):
4
We can use the graphemes
method from unicode-segmentation crate to iterate over the grapheme clusters and save them in a HashSet<&str>
to filter out the duplicates. Then we get the .len()
of the container.
extern crate unicode_segmentation; // 1.2.1
use std::collections::HashSet;
use unicode_segmentation::UnicodeSegmentation;
fn count_unique_grapheme_clusters(s: &str) -> usize {
let is_extended = true;
s.graphemes(is_extended).collect::<HashSet<_>>().len()
}
fn main() {
assert_eq!(count_unique_grapheme_clusters(""), 0);
assert_eq!(count_unique_grapheme_clusters("a"), 1);
assert_eq!(count_unique_grapheme_clusters("πΊπΈ"), 1);
assert_eq!(count_unique_grapheme_clusters("π·πΊeΜ"), 2);
assert_eq!(count_unique_grapheme_clusters("π§π·π·πΊπ§π·πΊπΈπ§π·"), 3);
}
Playground
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With