1 #![allow(dead_code)]
2 use std::{error::Error, io, process};
3
4 use serde::Deserialize;
5
6 // By default, struct field names are deserialized based on the position of
7 // a corresponding field in the CSV data's header record.
8 #[derive(Debug, Deserialize)]
9 struct Record {
10 city: String,
11 region: String,
12 country: String,
13 population: Option<u64>,
14 }
15
example() -> Result<(), Box<dyn Error>>16 fn example() -> Result<(), Box<dyn Error>> {
17 let mut rdr = csv::Reader::from_reader(io::stdin());
18 for result in rdr.deserialize() {
19 // Notice that we need to provide a type hint for automatic
20 // deserialization.
21 let record: Record = result?;
22 println!("{:?}", record);
23 }
24 Ok(())
25 }
26
main()27 fn main() {
28 if let Err(err) = example() {
29 println!("error running example: {}", err);
30 process::exit(1);
31 }
32 }
33