1 use std::{error::Error, io, process};
2 
3 use serde::Serialize;
4 
5 #[derive(Debug, Serialize)]
6 struct Record {
7     city: String,
8     region: String,
9     country: String,
10     population: Option<u64>,
11 }
12 
example() -> Result<(), Box<dyn Error>>13 fn example() -> Result<(), Box<dyn Error>> {
14     let mut wtr = csv::Writer::from_writer(io::stdout());
15 
16     // When writing records with Serde using structs, the header row is written
17     // automatically.
18     wtr.serialize(Record {
19         city: "Southborough".to_string(),
20         region: "MA".to_string(),
21         country: "United States".to_string(),
22         population: Some(9686),
23     })?;
24     wtr.serialize(Record {
25         city: "Northbridge".to_string(),
26         region: "MA".to_string(),
27         country: "United States".to_string(),
28         population: Some(14061),
29     })?;
30     wtr.flush()?;
31     Ok(())
32 }
33 
main()34 fn main() {
35     if let Err(err) = example() {
36         println!("error running example: {}", err);
37         process::exit(1);
38     }
39 }
40