Hello again.. I need to find an entry for each character in the string, but rustc gives error. What's wrong?
use std::collections::HashMap;
fn scounter(s: &str) -> HashMap<char, usize> {
let mut map: HashMap<char, usize> = HashMap::new();
for (i, c) in s.char_indices() {
match map.get_mut(&c) {
Some(x) => *x+=1,
None => map.insert(c, 1)
}
}
return map;
}
fn main() {
let x = "Hello!";
println!("{:?}", scounter(x));
}
Errors:
main.rs:7:3: 10:4 error: match arms have incompatible types:
expected (),
found core::option::Option<usize>
(expected (),
found enum core::option::Option) [E0308]
main.rs: 7 match map.get_mut(&c) {
main.rs: 8 Some(x) => *x+=1,
main.rs: 9 None => map.insert(c, 1)
main.rs:10 }
main.rs:7:3: 10:4 help: run rustc --explain E0308 to see a detailed explanation
main.rs:9:12: 9:28 note: match arm with an incompatible type
main.rs:9 None => map.insert(c, 1)
^~~~~~~~~~~~~~~~