Initial import of csv-1.1.5.
Bug: 155309706
Change-Id: Id08b46b0989d3b869c4a014d10ea98193b7c3b32
diff --git a/examples/tutorial-read-serde-01.rs b/examples/tutorial-read-serde-01.rs
new file mode 100644
index 0000000..a073157
--- /dev/null
+++ b/examples/tutorial-read-serde-01.rs
@@ -0,0 +1,35 @@
+use std::error::Error;
+use std::io;
+use std::process;
+
+fn run() -> Result<(), Box<dyn Error>> {
+ let mut rdr = csv::Reader::from_reader(io::stdin());
+ for result in rdr.records() {
+ let record = result?;
+
+ let city = &record[0];
+ let state = &record[1];
+ // Some records are missing population counts, so if we can't
+ // parse a number, treat the population count as missing instead
+ // of returning an error.
+ let pop: Option<u64> = record[2].parse().ok();
+ // Lucky us! Latitudes and longitudes are available for every record.
+ // Therefore, if one couldn't be parsed, return an error.
+ let latitude: f64 = record[3].parse()?;
+ let longitude: f64 = record[4].parse()?;
+
+ println!(
+ "city: {:?}, state: {:?}, \
+ pop: {:?}, latitude: {:?}, longitude: {:?}",
+ city, state, pop, latitude, longitude
+ );
+ }
+ Ok(())
+}
+
+fn main() {
+ if let Err(err) = run() {
+ println!("{}", err);
+ process::exit(1);
+ }
+}