1 use std::{error::Error, io, process};
2 
3 use serde::Serialize;
4 
5 // Note that structs can derive both Serialize and Deserialize!
6 #[derive(Debug, Serialize)]
7 #[serde(rename_all = "PascalCase")]
8 struct Record<'a> {
9     city: &'a str,
10     state: &'a str,
11     population: Option<u64>,
12     latitude: f64,
13     longitude: f64,
14 }
15 
run() -> Result<(), Box<dyn Error>>16 fn run() -> Result<(), Box<dyn Error>> {
17     let mut wtr = csv::Writer::from_writer(io::stdout());
18 
19     wtr.serialize(Record {
20         city: "Davidsons Landing",
21         state: "AK",
22         population: None,
23         latitude: 65.2419444,
24         longitude: -165.2716667,
25     })?;
26     wtr.serialize(Record {
27         city: "Kenai",
28         state: "AK",
29         population: Some(7610),
30         latitude: 60.5544444,
31         longitude: -151.2583333,
32     })?;
33     wtr.serialize(Record {
34         city: "Oakman",
35         state: "AL",
36         population: None,
37         latitude: 33.7133333,
38         longitude: -87.3886111,
39     })?;
40 
41     wtr.flush()?;
42     Ok(())
43 }
44 
main()45 fn main() {
46     if let Err(err) = run() {
47         println!("{}", err);
48         process::exit(1);
49     }
50 }
51