/
germanubis
/
rust-csv
Обзор
Документация
Войти
/
germanubis
/
rust-csv
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
examples/tutorial-write-serde-02.rs
50 строк
1 KB
Andrew Gallant
*: fix all warnings and update code
14 фев 2023, 05:31
14 фев 2023, 05:31
ea0273c
Код
Авторство
О чём код?
use std::{error::Error, io, process}; use serde::Serialize; // Note that structs can derive both Serialize and Deserialize! #[derive(Debug, Serialize)] #[serde(rename_all = "PascalCase")] struct Record<'a> { city: &'a str, state: &'a str, population: Option<u64>, latitude: f64, longitude: f64, } fn run() -> Result<(), Box<dyn Error>> { let mut wtr = csv::Writer::from_writer(io::stdout()); wtr.serialize(Record { city: "Davidsons Landing", state: "AK", population: None, latitude: 65.2419444, longitude: -165.2716667, })?; wtr.serialize(Record { city: "Kenai", state: "AK", population: Some(7610), latitude: 60.5544444, longitude: -151.2583333, })?; wtr.serialize(Record { city: "Oakman", state: "AL", population: None, latitude: 33.7133333, longitude: -87.3886111, })?; wtr.flush()?; Ok(()) } fn main() { if let Err(err) = run() { println!("{}", err); process::exit(1); } }