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