[![csv-badge]][csv] [![cat-encoding-badge]][cat-encoding]
用一个 tab(分隔符) delimiter
读取 csv 记录。
extern crate csv;
use csv::Error;
#[macro_use]
extern crate serde_derive;
#[derive(Debug, Deserialize)]
struct Record {
name: String,
place: String,
#[serde(deserialize_with = "csv::invalid_option")]
id: Option<u64>,
}
use csv::ReaderBuilder;
fn main() -> Result<(), Error> {
let data = "name\tplace\tid
Mark\tMelbourne\t46
Ashley\tZurich\t92";
let mut reader = ReaderBuilder::new().delimiter(b'\t').from_reader(data.as_bytes());
for result in reader.deserialize::<Record>() {
println!("{:?}", result?);
}
Ok(())
}