Hi Folks,
I am learning Rust, and attempting to create a simple program which counts phrases from a file (sample code below). This AddAssign
operation works fine if I attempt the example, but I am missing something with regards to ownership when I attempt to loop through the contents of the text.
From what I can gather, u64
does implement AddAssign
, so I'm not sure what I am missing here. Any guidance is greatly appreciated.
Error: error[E0368]: binary assignment operation
+=cannot be applied to type
&u64``
use std::collections::HashMap;
use std::io::prelude::*;
use std::io::{self, BufReader};
fn main() -> io::Result<()> {
let f = "This is a file with a newline.\nThis is a file without much in it.".as_bytes();
let f = BufReader::new(f);
let mut phraseMap = HashMap::new();
for line in f.lines() {
let v: Vec<String> = line.unwrap()
.split_whitespace()
.map(|s| s.to_string())
.collect();
let len = v.len();
let i = 0;
while i + 2 < len {
let count = phraseMap.entry(&v[i..i + 3].join(" ")).or_insert(&0);
*count += 1;
i = i + 1;
}
}
Ok(())
}