-
Notifications
You must be signed in to change notification settings - Fork 217
/
tutorial-perf-serde-02.rs
42 lines (38 loc) · 980 Bytes
/
tutorial-perf-serde-02.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
38
39
40
41
42
#![allow(dead_code)]
use serde::Deserialize;
use std::{error::Error, io, process};
#[derive(Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
struct Record<'a> {
country: &'a str,
city: &'a str,
accent_city: &'a str,
region: &'a str,
population: Option<u64>,
latitude: f64,
longitude: f64,
}
fn run() -> Result<u64, Box<dyn Error>> {
let mut rdr = csv::Reader::from_reader(io::stdin());
let mut raw_record = csv::StringRecord::new();
let headers = rdr.headers()?.clone();
let mut count = 0;
while rdr.read_record(&mut raw_record)? {
let record: Record = raw_record.deserialize(Some(&headers))?;
if record.country == "us" && record.region == "MA" {
count += 1;
}
}
Ok(count)
}
fn main() {
match run() {
Ok(count) => {
println!("{}", count);
}
Err(err) => {
println!("{}", err);
process::exit(1);
}
}
}