Skip to content

Commit 5b2da18

Browse files
authored
doc: simplify tutorial section on accessing headers
The ceremony around appeasing the borrow checker is no longer needed (thanks to NLL). PR #406
1 parent 633552a commit 5b2da18

File tree

2 files changed

+7
-33
lines changed

2 files changed

+7
-33
lines changed

examples/tutorial-read-headers-02.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,13 @@ use std::{error::Error, io, process};
22

33
fn run() -> Result<(), Box<dyn Error>> {
44
let mut rdr = csv::Reader::from_reader(io::stdin());
5-
{
6-
// We nest this call in its own scope because of lifetimes.
7-
let headers = rdr.headers()?;
8-
println!("{:?}", headers);
9-
}
5+
let headers = rdr.headers()?;
6+
println!("{:?}", headers);
107
for result in rdr.records() {
118
let record = result?;
129
println!("{:?}", record);
1310
}
14-
// We can ask for the headers at any time. There's no need to nest this
15-
// call in its own scope because we never try to borrow the reader again.
11+
// We can ask for the headers at any time.
1612
let headers = rdr.headers()?;
1713
println!("{:?}", headers);
1814
Ok(())

src/tutorial.rs

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ StringRecord(["Oakman", "AL", "", "33.7133333", "-87.3886111"])
555555
```
556556
557557
If 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)
559559
method like so:
560560
561561
```no_run
@@ -564,17 +564,13 @@ method like so:
564564
#
565565
fn 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
611589
In this section we'll temporarily depart from our `uspop.csv` data set and

0 commit comments

Comments
 (0)