Crate csv

source ·
Expand description

The csv crate provides a fast and flexible CSV reader and writer, with support for Serde.

The tutorial is a good place to start if you’re new to Rust.

The cookbook will give you a variety of complete Rust programs that do CSV reading and writing.

Brief overview

If you’re new to Rust, you might find the tutorial to be a good place to start.

The primary types in this crate are Reader and Writer, for reading and writing CSV data respectively. Correspondingly, to support CSV data with custom field or record delimiters (among many other things), you should use either a ReaderBuilder or a WriterBuilder, depending on whether you’re reading or writing CSV data.

Unless you’re using Serde, the standard CSV record types are StringRecord and ByteRecord. StringRecord should be used when you know your data to be valid UTF-8. For data that may be invalid UTF-8, ByteRecord is suitable.

Finally, the set of errors is described by the Error type.

The rest of the types in this crate mostly correspond to more detailed errors, position information, configuration knobs or iterator types.

Setup

Add this to your Cargo.toml:

[dependencies]
csv = "1.1"

If you want to use Serde’s custom derive functionality on your custom structs, then add this to your [dependencies] section of Cargo.toml:

[dependencies]
serde = { version = "1", features = ["derive"] }

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;
use std::io;
use std::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;
use std::io;
use std::process;

use serde::Deserialize;

#[derive(Debug, 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

Modules

A cookbook of examples for CSV reading and writing.
A tutorial for handling CSV data in Rust.

Structs

A single CSV record stored as raw bytes.
A double-ended iterator over the fields in a byte record.
An owned iterator over records as raw bytes.
A borrowed iterator over records as raw bytes.
An Serde deserialization error.
An owned iterator over deserialized records.
A borrowed iterator over deserialized records.
An error that can occur when processing CSV data.
A UTF-8 validation error during record conversion.
IntoInnerError occurs when consuming a Writer fails.
A position in CSV data.
A already configured CSV reader.
Builds a CSV reader with various configuration knobs.
A single CSV record stored as valid UTF-8 bytes.
An iterator over the fields in a string record.
An owned iterator over records as strings.
A borrowed iterator over records as strings.
A UTF-8 validation error.
A already configured CSV writer.
Builds a CSV writer with various configuration knobs.

Enums

The type of a Serde deserialization error.
The specific type of an error.
The quoting style to use when writing CSV data.
A record terminator.
The whitespace preservation behaviour when reading CSV data.

Functions

A custom Serde deserializer for possibly invalid Option<T> fields.

Type Definitions

A type alias for Result<T, csv::Error>.