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