diff --git a/solutions/rust/anagram/2/Cargo.toml b/solutions/rust/anagram/2/Cargo.toml new file mode 100644 index 0000000..5c8b0d1 --- /dev/null +++ b/solutions/rust/anagram/2/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "anagram" +version = "0.1.0" +edition = "2024" + +# Not all libraries from crates.io are available in Exercism's test runner. +# The full list of available libraries is here: +# https://github.com/exercism/rust-test-runner/blob/main/local-registry/Cargo.toml +[dependencies] +itertools = "0.14.0" +unicode-segmentation = "1.12.0" diff --git a/solutions/rust/anagram/2/src/lib.rs b/solutions/rust/anagram/2/src/lib.rs new file mode 100644 index 0000000..fd58e28 --- /dev/null +++ b/solutions/rust/anagram/2/src/lib.rs @@ -0,0 +1,17 @@ +use itertools::Itertools; +use std::collections::HashSet; +use unicode_segmentation::UnicodeSegmentation; + +pub fn anagrams_for<'a>(word: &str, possible_anagrams: &'a [&'a str]) -> HashSet<&'a str> { + let is_anagram = |input: &str| { + input.to_lowercase().graphemes(true).sorted().collect::() + == word.to_lowercase().graphemes(true).sorted().collect::() + && input.to_lowercase() != word.to_lowercase() + }; + + possible_anagrams + .iter() + .filter(|s| is_anagram(s)) + .cloned() + .collect() +}