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