A Person has a first_name and a last_name. How do I add a time of export to a CSV when writing an instance of Person to a CSV?
#[macro_use] extern crate serde_derive; extern crate csv; extern crate serde; use std::time::{SystemTime, UNIX_EPOCH}; #[derive(Debug, Serialize, Deserialize)] struct Person { first_name: String, last_name: String, } fn current_ms() -> u64 { let since_the_epoch = &SystemTime::now().duration_since(UNIX_EPOCH).unwrap(); let ms = since_the_epoch.as_secs() * 1000 + since_the_epoch.subsec_nanos() as u64 / 1_000_000; ms } fn main() { let person = Person { first_name: "Craig".to_string(), last_name: "David".to_string(), }; let file_path = std::path::Path::new("test.csv"); let mut wtr = csv::Writer::from_path(file_path).unwrap(); wtr.serialize(&person).unwrap(); println!("{}", current_ms()); } The above code results in:
>>> cat test.csv first_name,last_name Craig,David My objective is to generate a file which is in the format:
>>> cat test.csv time,first_name,last_name 1527661792118,Craig,David Serde attributes do not have an option to add an additional field. In the above example the Person struct does not contain a time value. The time value is only required when the data is written to a CSV.