You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
use std::{error::Error, io, process};fnexample() -> Result<(),Box<dynError>>{// Build the CSV reader and iterate over each record.letmut rdr = csv::Reader::from_reader(io::stdin());for result in rdr.records(){// The iterator yields Result<StringRecord, Error>, so we check the// error here.let record = result?;println!("{:?}", record);}Ok(())}fnmain(){ifletErr(err) = example(){println!("error running example: {}", err);
process::exit(1);}}
The above example can be run like so:
$ git clone git://github.com/BurntSushi/rust-csv
$ cd rust-csv
$ cargo run --example cookbook-read-basic < examples/data/smallpop.csv
Example with Serde
This example shows how to read CSV data from stdin into your own custom struct.
By default, the member names of the struct are matched with the values in the
header record of your CSV data.
use std::{error::Error, io, process};#[derive(Debug, serde::Deserialize)]structRecord{city:String,region:String,country:String,population:Option<u64>,}fnexample() -> Result<(),Box<dynError>>{letmut rdr = csv::Reader::from_reader(io::stdin());for result in rdr.deserialize(){// Notice that we need to provide a type hint for automatic// deserialization.let record:Record = result?;println!("{:?}", record);}Ok(())}fnmain(){ifletErr(err) = example(){println!("error running example: {}", err);
process::exit(1);}}
The above example can be run like so:
$ git clone git://github.com/BurntSushi/rust-csv
$ cd rust-csv
$ cargo run --example cookbook-read-serde < examples/data/smallpop.csv