Skip to content

Instantly share code, notes, and snippets.

@jmoyers
Last active May 26, 2016 23:53
Show Gist options
  • Save jmoyers/b212710b776a050a1c8030bf3d23c387 to your computer and use it in GitHub Desktop.
Save jmoyers/b212710b776a050a1c8030bf3d23c387 to your computer and use it in GitHub Desktop.
// From https://thesquareplanet.com/blog/the-path-to-rust/
// Example 2: non-functional
let mut idx = HashMap::new();
for fname in &args {
let f = match fs::File::open(fname) {
Ok(f) => io::BufReader::new(f),
Err(e) => panic!("input file {} could not be opened: {}", fname, e),
};
let mut words = HashSet::new();
for line in f.lines() {
for w in line.unwrap().split_whitespace() {
if words.insert(w.to_string()) {
// new word seen
idx.entry(w.to_string()).or_insert(Vec::new()).push(fname);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment