Initial import of csv-1.1.5.
Bug: 155309706
Change-Id: Id08b46b0989d3b869c4a014d10ea98193b7c3b32
diff --git a/examples/cookbook-read-serde.rs b/examples/cookbook-read-serde.rs
new file mode 100644
index 0000000..420e0f7
--- /dev/null
+++ b/examples/cookbook-read-serde.rs
@@ -0,0 +1,33 @@
+use std::error::Error;
+use std::io;
+use std::process;
+
+use serde::Deserialize;
+
+// By default, struct field names are deserialized based on the position of
+// a corresponding field in the CSV data's header record.
+#[derive(Debug, 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);
+ }
+}