Skip to content

Instantly share code, notes, and snippets.

@gregkatz
Created May 26, 2017 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gregkatz/39ee2be6585c81874f83ca7712fc54f5 to your computer and use it in GitHub Desktop.
Save gregkatz/39ee2be6585c81874f83ca7712fc54f5 to your computer and use it in GitHub Desktop.
extern crate csv;
use std::error::Error;
use std::collections::HashMap;
use csv::ReaderBuilder;
fn main() {
match run() {
Ok(_) => {},
Err(e) => println!("Error: {:?}", e),
};
}
fn run() -> Result<(), Box<Error>> {
let mut map: HashMap<String, i64> = HashMap::new();
let mut reader = ReaderBuilder::new()
.delimiter(b'\t')
.from_path("./googledata")?;
for record in reader.records() {
let record = record?;
*map.entry(record[1].into()).or_insert(0) += record[2].parse()?;
}
if let Some((max_key, max_val)) = map.iter().max_by_key(|&(_, v)| v) {
println!("max_key: {} sum: {}", max_key, max_val);
} else {
println!("No entries");
}
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment