1 #![allow(dead_code)]
2 use std::{error::Error, io, process};
3
4 // This lets us write `#[derive(Deserialize)]`.
5 use serde::Deserialize;
6
7 // We don't need to derive `Debug` (which doesn't require Serde), but it's a
8 // good habit to do it for all your types.
9 //
10 // Notice that the field names in this struct are NOT in the same order as
11 // the fields in the CSV data!
12 #[derive(Debug, Deserialize)]
13 #[serde(rename_all = "PascalCase")]
14 struct Record {
15 latitude: f64,
16 longitude: f64,
17 population: Option<u64>,
18 city: String,
19 state: String,
20 }
21
run() -> Result<(), Box<dyn Error>>22 fn run() -> Result<(), Box<dyn Error>> {
23 let mut rdr = csv::Reader::from_reader(io::stdin());
24 for result in rdr.deserialize() {
25 let record: Record = result?;
26 println!("{:?}", record);
27 // Try this if you don't like each record smushed on one line:
28 // println!("{:#?}", record);
29 }
30 Ok(())
31 }
32
main()33 fn main() {
34 if let Err(err) = run() {
35 println!("{}", err);
36 process::exit(1);
37 }
38 }
39