• Home
Name
Date
Size
#Lines
LOC

..--

benches/03-May-2024-527492

examples/03-May-2024-141,508141,223

src/03-May-2024-12,2035,010

tests/03-May-2024-512435

.cargo_vcs_info.jsonD03-May-202494 66

.gitignoreD03-May-202488 109

Android.bpD03-May-20241.9 KiB6157

COPYINGD03-May-2024126 42

Cargo.tomlD03-May-20241.4 KiB7463

Cargo.toml.origD03-May-2024960 3932

ISSUE_TEMPLATE.mdD03-May-20241.3 KiB3823

LICENSED03-May-20242.2 KiB4837

LICENSE-MITD03-May-20241.1 KiB2217

METADATAD03-May-2024596 2422

MODULE_LICENSE_MITD03-May-20240

OWNERSD03-May-202440 21

README.mdD03-May-20242.5 KiB11184

TEST_MAPPINGD03-May-2024337 1817

UNLICENSED03-May-20241.2 KiB2520

cargo2android.jsonD03-May-202435 44

rustfmt.tomlD03-May-202444 32

README.md

1csv
2===
3A fast and flexible CSV reader and writer for Rust, with support for Serde.
4
5[![Build status](https://github.com/BurntSushi/csv/workflows/ci/badge.svg)](https://github.com/BurntSushi/csv/actions)
6[![crates.io](https://img.shields.io/crates/v/csv.svg)](https://crates.io/crates/csv)
7
8Dual-licensed under MIT or the [UNLICENSE](http://unlicense.org).
9
10
11### Documentation
12
13https://docs.rs/csv
14
15If you're new to Rust, the
16[tutorial](https://docs.rs/csv/1.0.0/csv/tutorial/index.html)
17is a good place to start.
18
19
20### Usage
21
22Add this to your `Cargo.toml`:
23
24```toml
25[dependencies]
26csv = "1.2"
27```
28
29### Example
30
31This example shows how to read CSV data from stdin and print each record to
32stdout.
33
34There are more examples in the
35[cookbook](https://docs.rs/csv/1.0.0/csv/cookbook/index.html).
36
37```rust
38use std::{error::Error, io, process};
39
40fn example() -> Result<(), Box<dyn Error>> {
41    // Build the CSV reader and iterate over each record.
42    let mut rdr = csv::Reader::from_reader(io::stdin());
43    for result in rdr.records() {
44        // The iterator yields Result<StringRecord, Error>, so we check the
45        // error here.
46        let record = result?;
47        println!("{:?}", record);
48    }
49    Ok(())
50}
51
52fn main() {
53    if let Err(err) = example() {
54        println!("error running example: {}", err);
55        process::exit(1);
56    }
57}
58```
59
60The above example can be run like so:
61
62```text
63$ git clone git://github.com/BurntSushi/rust-csv
64$ cd rust-csv
65$ cargo run --example cookbook-read-basic < examples/data/smallpop.csv
66```
67
68### Example with Serde
69
70This example shows how to read CSV data from stdin into your own custom struct.
71By default, the member names of the struct are matched with the values in the
72header record of your CSV data.
73
74```rust
75use std::{error::Error, io, process};
76
77#[derive(Debug, serde::Deserialize)]
78struct Record {
79    city: String,
80    region: String,
81    country: String,
82    population: Option<u64>,
83}
84
85fn example() -> Result<(), Box<dyn Error>> {
86    let mut rdr = csv::Reader::from_reader(io::stdin());
87    for result in rdr.deserialize() {
88        // Notice that we need to provide a type hint for automatic
89        // deserialization.
90        let record: Record = result?;
91        println!("{:?}", record);
92    }
93    Ok(())
94}
95
96fn main() {
97    if let Err(err) = example() {
98        println!("error running example: {}", err);
99        process::exit(1);
100    }
101}
102```
103
104The above example can be run like so:
105
106```text
107$ git clone git://github.com/BurntSushi/rust-csv
108$ cd rust-csv
109$ cargo run --example cookbook-read-serde < examples/data/smallpop.csv
110```
111