我建立一個簡單的程序,其在直到它到達文件的結束的時間中讀取標準輸入一行時,則打印每個字母的頻率(即:字符,實際上技術上的Unicode字形)輸入。 The full source is on Github。我正在使用rustc版本1.6.0和貨物0.7.0不匹配的類型的錯誤插入一個HashMap <&str, u64>
在程序中,我定義了一個HashMap<&str, u64>
來存儲統計信息,使用字母作爲關鍵字並將該字母的出現次數作爲值。雖然通過循環每個字母,我下面來存儲數據:
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)
}
}
}
(其中grapheme
is a reference to a string)。
我知道這可能不是更新HashMap中的計數器最好的方式,但我相信它應該在技術上的工作---我是一個總鏽病的n00b畢竟。
當我cargo build
,我得到:
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 }
...從看文檔的E0308,以及確切的錯誤消息,據我所知,編譯器越來越一種類型和期待另一個;但我不明白:
- 我是否看到兩個錯配或一個,即:
- 有
core::option::Option<u64>
和core::option::Option
之間的不匹配? - 是否有兩個不匹配,在
()
和core::option::Option<u64>
之間,以及在()
和core::option::Option
之間? - 別的東西?
- 有
- 我不明白如何告訴Rust的編譯器如何用正確的類型解釋事情(即:如何解決問題)。
僅供參考,一旦你解決這個問題,你很可能會碰上[問題解決了這裏(http://stackoverflow.com/q/28512394/155423)。 – Shepmaster