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