aboutsummaryrefslogtreecommitdiff
path: root/examples/tutorial-read-headers-02.rs
diff options
context:
space:
mode:
Diffstat (limited to 'examples/tutorial-read-headers-02.rs')
-rw-r--r--examples/tutorial-read-headers-02.rs28
1 files changed, 28 insertions, 0 deletions
diff --git a/examples/tutorial-read-headers-02.rs b/examples/tutorial-read-headers-02.rs
new file mode 100644
index 0000000..ddaa756
--- /dev/null
+++ b/examples/tutorial-read-headers-02.rs
@@ -0,0 +1,28 @@
+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());
+ {
+ // We nest this call in its own scope because of lifetimes.
+ let headers = rdr.headers()?;
+ println!("{:?}", headers);
+ }
+ for result in rdr.records() {
+ let record = result?;
+ println!("{:?}", record);
+ }
+ // We can ask for the headers at any time. There's no need to nest this
+ // call in its own scope because we never try to borrow the reader again.
+ let headers = rdr.headers()?;
+ println!("{:?}", headers);
+ Ok(())
+}
+
+fn main() {
+ if let Err(err) = run() {
+ println!("{}", err);
+ process::exit(1);
+ }
+}