| use std::{error::Error, io, process}; | |
| fn run() -> Result<(), Box<dyn Error>> { | |
| let mut rdr = csv::Reader::from_reader(io::stdin()); | |
| for result in rdr.records() { | |
| let record = result?; | |
| let city = &record[0]; | |
| let state = &record[1]; | |
| // Some records are missing population counts, so if we can't | |
| // parse a number, treat the population count as missing instead | |
| // of returning an error. | |
| let pop: Option<u64> = record[2].parse().ok(); | |
| // Lucky us! Latitudes and longitudes are available for every record. | |
| // Therefore, if one couldn't be parsed, return an error. | |
| let latitude: f64 = record[3].parse()?; | |
| let longitude: f64 = record[4].parse()?; | |
| println!( | |
| "city: {:?}, state: {:?}, \ | |
| pop: {:?}, latitude: {:?}, longitude: {:?}", | |
| city, state, pop, latitude, longitude | |
| ); | |
| } | |
| Ok(()) | |
| } | |
| fn main() { | |
| if let Err(err) = run() { | |
| println!("{}", err); | |
| process::exit(1); | |
| } | |
| } |