use regex::Regex;
use std::collections::HashMap;
/// Count occurrences of words.
pub fn word_count(words: &str) -> HashMap<String, u32> {
let word_re = Regex::new(r"\d+|([a-zA-Z]+(?:'??[a-zA-Z]+))").unwrap();
words
.split_whitespace()
.flat_map(|w| word_re.find_iter(w))
.map(|w| w.as_str().to_lowercase())
.fold(HashMap::new(), |mut freq, w| {
*freq.entry(w).or_default() += 1;
freq
})
}
flat_map(word_re::find_iter) fails compilation with "use of undeclared crate or module word_re". What is the correct syntax for using a method reference from a local variable?
That's unexpected, languages like Java can do it just fine. word_re does have an associated method find_iter. That said, I'm just learning, so, I'm going to let it slide as a design limitation.
Note that, although it doesn't lead to the concision you were looking for, it is possible to call find_iter like this:
Regex::find_iter(&word_re, w)
If you want to read more about this way of calling methods, it is called "Universal Function Call Syntax"
For the special case of a method without any extra parameters, you can use this syntax where you might otherwise use a closure such as str::to_lowercase in this version of part of your code: