I am creating a simple program which reads stdin one line at a time until it reaches the end of the file, then prints the frequency of each letter (i.e.: character, actually technically Unicode Graphemes) in the input. The full source is on Github. I'm using rustc version 1.6.0 and cargo 0.7.0
In the program, I define a HashMap<&str, u64>
to store the statistics, using the letter as the key and the number of occurrences of that letter as the value. While looping through each letter, I do the following to store the statistics:
for line in stdin.lock().lines() {
let mut line = line.unwrap().as_str();
// For each line, store it's character.
for grapheme in UnicodeSegmentation::graphemes(line, true) {
match counter.get(grapheme) {
Some(v) => counter.insert(grapheme, v + 1),
None => counter.insert(grapheme, 1)
}
}
}
(where grapheme
is a reference to a string).
I realize this might not be the best way to update the counters in the hashmap, but I believe it should technically work --- I am a total Rust n00b after all.
When I cargo build
, I get:
expected `()`,
found `core::option::Option<u64>`
(expected (),
found enum `core::option::Option`) [E0308]
src/main.rs:18 match counter.get(grapheme) {
src/main.rs:19 Some(v) => counter.insert(grapheme, v + 1),
src/main.rs:20 None => counter.insert(grapheme, 1)
src/main.rs:21 }
... from looking at the docs for E0308, and the exact error message, I understand the compiler is getting one type and expecting another; but I don't understand:
- whether I'm seeing two mismatches or one, i.e.:
- is there a mismatch between
core::option::Option<u64>
andcore::option::Option
? - are there two mismatches, between
()
andcore::option::Option<u64>
and between()
andcore::option::Option
? - something else?
- is there a mismatch between
- I don't understand how to tell Rust's compiler how to interpret things with the correct type (i.e.: what to do to fix the issue).