diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-18 02:49:42 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-18 02:49:42 +0000 |
commit | 837b550238aa671a591ccf282dddeab29cadb206 (patch) | |
tree | 914b6b8862bace72bd3245ca184d374b08d8a672 /vendor/csv/README.md | |
parent | Adding debian version 1.70.0+dfsg2-1. (diff) | |
download | rustc-837b550238aa671a591ccf282dddeab29cadb206.tar.xz rustc-837b550238aa671a591ccf282dddeab29cadb206.zip |
Merging upstream version 1.71.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/csv/README.md')
-rw-r--r-- | vendor/csv/README.md | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/vendor/csv/README.md b/vendor/csv/README.md new file mode 100644 index 000000000..e1d844302 --- /dev/null +++ b/vendor/csv/README.md @@ -0,0 +1,110 @@ +csv +=== +A fast and flexible CSV reader and writer for Rust, with support for Serde. + +[](https://github.com/BurntSushi/rust-csv/actions) +[](https://crates.io/crates/csv) + +Dual-licensed under MIT or the [UNLICENSE](http://unlicense.org). + + +### Documentation + +https://docs.rs/csv + +If you're new to Rust, the +[tutorial](https://docs.rs/csv/1.0.0/csv/tutorial/index.html) +is a good place to start. + + +### Usage + +Add this to your `Cargo.toml`: + +```toml +[dependencies] +csv = "1.2" +``` + +### Example + +This example shows how to read CSV data from stdin and print each record to +stdout. + +There are more examples in the +[cookbook](https://docs.rs/csv/1.0.0/csv/cookbook/index.html). + +```rust +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: + +```text +$ 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. + +```rust +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: + +```text +$ git clone git://github.com/BurntSushi/rust-csv +$ cd rust-csv +$ cargo run --example cookbook-read-serde < examples/data/smallpop.csv +``` |