]> git.proxmox.com Git - rustc.git/blob - vendor/csv/examples/tutorial-write-serde-01.rs
New upstream version 1.71.1+dfsg1
[rustc.git] / vendor / csv / examples / tutorial-write-serde-01.rs
1 use std::{error::Error, io, process};
2
3 fn run() -> Result<(), Box<dyn Error>> {
4 let mut wtr = csv::Writer::from_writer(io::stdout());
5
6 // We still need to write headers manually.
7 wtr.write_record(&[
8 "City",
9 "State",
10 "Population",
11 "Latitude",
12 "Longitude",
13 ])?;
14
15 // But now we can write records by providing a normal Rust value.
16 //
17 // Note that the odd `None::<u64>` syntax is required because `None` on
18 // its own doesn't have a concrete type, but Serde needs a concrete type
19 // in order to serialize it. That is, `None` has type `Option<T>` but
20 // `None::<u64>` has type `Option<u64>`.
21 wtr.serialize((
22 "Davidsons Landing",
23 "AK",
24 None::<u64>,
25 65.2419444,
26 -165.2716667,
27 ))?;
28 wtr.serialize(("Kenai", "AK", Some(7610), 60.5544444, -151.2583333))?;
29 wtr.serialize(("Oakman", "AL", None::<u64>, 33.7133333, -87.3886111))?;
30
31 wtr.flush()?;
32 Ok(())
33 }
34
35 fn main() {
36 if let Err(err) = run() {
37 println!("{}", err);
38 process::exit(1);
39 }
40 }