Dear Rustaceans,
I am trying to make a program that reads a string of characters, divides it into words based on some delimiter and then performs to_lowercase() function to each word and add it to a HashMap object with its corresponding count in the string.
Below you could see a part of the program I have trouble with:
'use std::collections::HashMap;
fn main() {
// let buf = "тест Тест test tEst TESt";
let buf = "doG dOG DOg DOG собАка сОБАкА";
let mut words: HashMap<&str, usize> = HashMap::new();
for word in buf.split(is_delimiter) {
let lowercase = word.to_lowercase();
let lower = &*lowercase;
print!("{}, ", lower);
if let Some(count) = words.get_mut(lower) {
*count += 1;
continue;
}
words.insert(word, 1);
}
println!("{:?}", words);
}
fn is_delimiter(c: char) -> bool {
let delimiter = [
' ',
'\t',
'\n',
];
for i in delimiter.iter() {
if c == *i {
return true;
}
}
false
}'
If you run the program it provides the expected result - all test words (both Latin and Cyrillic) are converted to lowercase and HashMap counts correctly 2 times Cyrillic and 3 times Latin version of test.
However, when I try to run it with the second buf - with dogs (commenting out the first one), the result of lower variable is the expected one, however, when using the get_mut() method it puts the original case of the words into the HashMaps.
I have also tested this on larger pieces of text and sometimes it adds words correctly, while sometimes - not and I cannot see the exact reason for that.
Hope you can help me out.