blob: a073157f7b4c353f77e446737ded835dd91a8ab1 [file] [log] [blame]
Jakub Koturc72d7202020-12-21 17:28:15 +01001use std::error::Error;
2use std::io;
3use std::process;
4
5fn run() -> Result<(), Box<dyn Error>> {
6 let mut rdr = csv::Reader::from_reader(io::stdin());
7 for result in rdr.records() {
8 let record = result?;
9
10 let city = &record[0];
11 let state = &record[1];
12 // Some records are missing population counts, so if we can't
13 // parse a number, treat the population count as missing instead
14 // of returning an error.
15 let pop: Option<u64> = record[2].parse().ok();
16 // Lucky us! Latitudes and longitudes are available for every record.
17 // Therefore, if one couldn't be parsed, return an error.
18 let latitude: f64 = record[3].parse()?;
19 let longitude: f64 = record[4].parse()?;
20
21 println!(
22 "city: {:?}, state: {:?}, \
23 pop: {:?}, latitude: {:?}, longitude: {:?}",
24 city, state, pop, latitude, longitude
25 );
26 }
27 Ok(())
28}
29
30fn main() {
31 if let Err(err) = run() {
32 println!("{}", err);
33 process::exit(1);
34 }
35}