tag | 16b0630015630bed6c52a347d11602656f7fc1c2 | |
---|---|---|
tagger | The Android Open Source Project <initial-contribution@android.com> | Mon May 06 21:48:10 2024 -0700 |
object | 0584c63ecce076fff11e0f63855131edfa6f4326 |
Android platform 14.0.0 release 7
commit | 0584c63ecce076fff11e0f63855131edfa6f4326 | [log] [tgz] |
---|---|---|
author | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | Fri Mar 10 02:19:42 2023 +0000 |
committer | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | Fri Mar 10 02:19:42 2023 +0000 |
tree | 54a419676109a710f3f0e89202a8dbacf17ddd18 | |
parent | 80c5ca4818707fc2a7ee03159d805dbf1eef97e4 [diff] | |
parent | 2561a4b4bd9939395f488d19218754803353d3a0 [diff] |
Snap for 9719949 from 2561a4b4bd9939395f488d19218754803353d3a0 to udc-release Change-Id: I998d6f762ed06cbb1d028231547879151e0a6c59
A fast and flexible CSV reader and writer for Rust, with support for Serde.
Dual-licensed under MIT or the UNLICENSE.
If you're new to Rust, the tutorial is a good place to start.
Add this to your Cargo.toml
:
[dependencies] csv = "1.2"
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
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