1 use std::{error::Error, io, process};
2
run() -> Result<(), Box<dyn Error>>3 fn run() -> Result<(), Box<dyn Error>> {
4 let mut wtr = csv::Writer::from_writer(io::stdout());
5 // Since we're writing records manually, we must explicitly write our
6 // header record. A header record is written the same way that other
7 // records are written.
8 wtr.write_record(&[
9 "City",
10 "State",
11 "Population",
12 "Latitude",
13 "Longitude",
14 ])?;
15 wtr.write_record(&[
16 "Davidsons Landing",
17 "AK",
18 "",
19 "65.2419444",
20 "-165.2716667",
21 ])?;
22 wtr.write_record(&["Kenai", "AK", "7610", "60.5544444", "-151.2583333"])?;
23 wtr.write_record(&["Oakman", "AL", "", "33.7133333", "-87.3886111"])?;
24
25 // A CSV writer maintains an internal buffer, so it's important
26 // to flush the buffer when you're done.
27 wtr.flush()?;
28 Ok(())
29 }
30
main()31 fn main() {
32 if let Err(err) = run() {
33 println!("{}", err);
34 process::exit(1);
35 }
36 }
37