O'Reilly logo

Mastering Rust by Vesa Kaihlavirta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Exercise - fix the word counter

Here's a working version of the word counter. We have added the missing use statement and missing types, missing pointer syntax, and a missing mutability flag:

use std::env;use std::fs::File; use std::io::prelude::BufRead;use std::io::BufReader;use std::collections::HashMap; #[derive(Debug)]struct WordStore (HashMap<String, u64>);  impl WordStore {    fn new() -> WordStore {        WordStore (HashMap::new())     }     fn increment(&mut self, word: &str) {        let key = word.to_string();        let count = self.0.entry(key).or_insert(0);        *count += 1;    }     fn display(&self) {        for (key, value) in self.0.iter() {            println!("{}: {}", key, value);        }    }}  fn main() {     let arguments: Vec<String> = env::args().collect();  println!("args 1 {}", arguments[1]); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required