-
Notifications
You must be signed in to change notification settings - Fork 217
/
tutorial-read-serde-01.rs
37 lines (32 loc) · 1.05 KB
/
tutorial-read-serde-01.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
extern crate csv;
use std::error::Error;
use std::io;
use std::process;
fn run() -> Result<(), Box<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);
}
}