MCPcopy Index your code
hub / github.com/BurntSushi/rust-csv

github.com/BurntSushi/rust-csv @1.4.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release 1.4.0 ↗ · + Follow
793 symbols 2,085 edges 55 files 153 documented · 19%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

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

Extension points exported contracts — how you extend this code

DeRecord (Interface)
An over-engineered internal trait that permits writing a single Serde deserializer that works on both ByteRecord and Str [3 …
src/deserializer.rs

Core symbols most depended-on inside this repo

len
called by 41
csv-index/src/simple.rs
clone
called by 40
csv-core/src/writer.rs
serialize
called by 36
src/serializer.rs
write_record
called by 34
src/writer.rs
push_field
called by 33
src/byte_record.rs
len
called by 32
src/byte_record.rs
serialize
called by 31
src/writer.rs
serialize_header
called by 30
src/serializer.rs

Shape

Function 357
Method 340
Class 74
Enum 21
Interface 1

Languages

Rust100%

Modules by API surface

src/deserializer.rs106 symbols
src/serializer.rs85 symbols
src/reader.rs79 symbols
src/byte_record.rs79 symbols
csv-core/src/reader.rs72 symbols
src/writer.rs60 symbols
csv-core/src/writer.rs59 symbols
tests/tests.rs57 symbols
src/string_record.rs44 symbols
src/error.rs21 symbols
benches/bench.rs18 symbols
csv-index/src/simple.rs15 symbols

For agents

$ claude mcp add rust-csv \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact