Bug: 179101377

Clone this repo:
  1. 337ee94 Upgrade csv to 1.3.0 am: 54f5cc4cdf by Jeff Vander Stoep · 3 months ago main master
  2. 54f5cc4 Upgrade csv to 1.3.0 by Jeff Vander Stoep · 3 months ago
  3. bd98067 Migrate to cargo_embargo. am: 8ddb6a7b53 am: cb987c7ba3 am: ab77bbb5ab by Andrew Walbran · 5 months ago
  4. 5d0a086 Migrate to cargo_embargo. am: 8ddb6a7b53 am: 80d82cf821 am: 00d21fc242 by Andrew Walbran · 5 months ago
  5. ab77bbb Migrate to cargo_embargo. am: 8ddb6a7b53 am: cb987c7ba3 by Andrew Walbran · 5 months ago

csv

A fast and flexible CSV reader and writer for Rust, with support for Serde.

Build status crates.io

Dual-licensed under MIT or the UNLICENSE.

Documentation

https://docs.rs/csv

If you're new to Rust, the tutorial is a good place to start.

Usage

To bring this crate into your repository, either add csv to your Cargo.toml, or run cargo add csv.

Example

This example shows how to read CSV data from stdin and print each record to stdout.

There are more examples in the cookbook.

use std::{error::Error, io, process};

fn example() -> Result<(), Box<dyn Error>> {
    // Build the CSV reader and iterate over each record.
    let mut 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(())
}

fn main() {
    if let Err(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)]
struct Record {
    city: String,
    region: String,
    country: String,
    population: Option<u64>,
}

fn example() -> Result<(), Box<dyn Error>> {
    let mut 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(())
}

fn main() {
    if let Err(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