Initial import of csv-1.1.5.
Bug: 155309706
Change-Id: Id08b46b0989d3b869c4a014d10ea98193b7c3b32
diff --git a/examples/tutorial-read-01.rs b/examples/tutorial-read-01.rs
new file mode 100644
index 0000000..9fc9293
--- /dev/null
+++ b/examples/tutorial-read-01.rs
@@ -0,0 +1,32 @@
+use std::env;
+use std::error::Error;
+use std::ffi::OsString;
+use std::fs::File;
+use std::process;
+
+fn run() -> Result<(), Box<dyn Error>> {
+ let file_path = get_first_arg()?;
+ let file = File::open(file_path)?;
+ let mut rdr = csv::Reader::from_reader(file);
+ for result in rdr.records() {
+ let record = result?;
+ println!("{:?}", record);
+ }
+ Ok(())
+}
+
+/// Returns the first positional argument sent to this process. If there are no
+/// positional arguments, then this returns an error.
+fn get_first_arg() -> Result<OsString, Box<dyn Error>> {
+ match env::args_os().nth(1) {
+ None => Err(From::from("expected 1 argument, but got none")),
+ Some(file_path) => Ok(file_path),
+ }
+}
+
+fn main() {
+ if let Err(err) = run() {
+ println!("{}", err);
+ process::exit(1);
+ }
+}