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