Initial import of csv-1.1.5.
Bug: 155309706
Change-Id: Id08b46b0989d3b869c4a014d10ea98193b7c3b32
diff --git a/examples/tutorial-read-serde-04.rs b/examples/tutorial-read-serde-04.rs
new file mode 100644
index 0000000..e01d355
--- /dev/null
+++ b/examples/tutorial-read-serde-04.rs
@@ -0,0 +1,39 @@
+use std::error::Error;
+use std::io;
+use std::process;
+
+// This lets us write `#[derive(Deserialize)]`.
+use serde::Deserialize;
+
+// We don't need to derive `Debug` (which doesn't require Serde), but it's a
+// good habit to do it for all your types.
+//
+// Notice that the field names in this struct are NOT in the same order as
+// the fields in the CSV data!
+#[derive(Debug, Deserialize)]
+#[serde(rename_all = "PascalCase")]
+struct Record {
+ latitude: f64,
+ longitude: f64,
+ population: Option<u64>,
+ city: String,
+ state: String,
+}
+
+fn run() -> Result<(), Box<dyn Error>> {
+ let mut rdr = csv::Reader::from_reader(io::stdin());
+ for result in rdr.deserialize() {
+ let record: Record = result?;
+ println!("{:?}", record);
+ // Try this if you don't like each record smushed on one line:
+ // println!("{:#?}", record);
+ }
+ Ok(())
+}
+
+fn main() {
+ if let Err(err) = run() {
+ println!("{}", err);
+ process::exit(1);
+ }
+}