@@ -555,7 +555,7 @@ StringRecord(["Oakman", "AL", "", "33.7133333", "-87.3886111"])
555555```
556556
557557If you ever need to access the header record directly, then you can use the
558- [`Reader::header `](../struct.Reader.html#method.headers)
558+ [`Reader::headers `](../struct.Reader.html#method.headers)
559559method like so:
560560
561561```no_run
@@ -564,17 +564,13 @@ method like so:
564564#
565565fn run() -> Result<(), Box<dyn Error>> {
566566 let mut rdr = csv::Reader::from_reader(io::stdin());
567- {
568- // We nest this call in its own scope because of lifetimes.
569- let headers = rdr.headers()?;
570- println!("{:?}", headers);
571- }
567+ let headers = rdr.headers()?;
568+ println!("{:?}", headers);
572569 for result in rdr.records() {
573570 let record = result?;
574571 println!("{:?}", record);
575572 }
576- // We can ask for the headers at any time. There's no need to nest this
577- // call in its own scope because we never try to borrow the reader again.
573+ // We can ask for the headers at any time.
578574 let headers = rdr.headers()?;
579575 println!("{:?}", headers);
580576 Ok(())
@@ -588,24 +584,6 @@ fn run() -> Result<(), Box<dyn Error>> {
588584# }
589585```
590586
591- One interesting thing to note in this example is that we put the call to
592- `rdr.headers()` in its own scope. We do this because `rdr.headers()` returns
593- a *borrow* of the reader's internal header state. The nested scope in this
594- code allows the borrow to end before we try to iterate over the records. If
595- we didn't nest the call to `rdr.headers()` in its own scope, then the code
596- wouldn't compile because we cannot borrow the reader's headers at the same time
597- that we try to borrow the reader to iterate over its records.
598-
599- Another way of solving this problem is to *clone* the header record:
600-
601- ```ignore
602- let headers = rdr.headers()?.clone();
603- ```
604-
605- This converts it from a borrow of the CSV reader to a new owned value. This
606- makes the code a bit easier to read, but at the cost of copying the header
607- record into a new allocation.
608-
609587## Delimiters, quotes and variable length records
610588
611589In this section we'll temporarily depart from our `uspop.csv` data set and
0 commit comments