From f4eefafd6afb0edf51c776e14179783aed265779 Mon Sep 17 00:00:00 2001 From: Michael Milton Date: Mon, 16 Aug 2021 01:32:22 +1000 Subject: [PATCH 1/2] Add slightly more detail about StringRecord in examples --- README.md | 6 +++++- src/cookbook.rs | 10 ++++++++-- src/lib.rs | 6 +++++- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index b1c5727d..ce46abe2 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,11 @@ fn example() -> Result<(), Box> { // The iterator yields Result, so we check the // error here. let record = result?; - println!("{:?}", record); + + // Records can be indexed like a vector to return single strings + let cell: &str = &record[0]; + + println!("The first cell is {:?}", cell); } Ok(()) } diff --git a/src/cookbook.rs b/src/cookbook.rs index a28dc723..1eca694c 100644 --- a/src/cookbook.rs +++ b/src/cookbook.rs @@ -40,9 +40,13 @@ fn example() -> Result<(), Box> { let mut rdr = csv::Reader::from_reader(io::stdin()); for result in rdr.records() { // The iterator yields Result, so we check the - // error here.. + // error here. let record = result?; - println!("{:?}", record); + + // Records can be indexed like a vector to return single strings + let cell: &str = &record[0]; + + println!("The first cell is {:?}", cell); } Ok(()) } @@ -55,6 +59,8 @@ fn main() { } ``` +For more information on how to use a `StringRecord` object, refer its [API page](../struct.StringRecord.html). + The above example can be run like so: ```ignore diff --git a/src/lib.rs b/src/lib.rs index 3c771c95..f5001aea 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -76,7 +76,11 @@ fn example() -> Result<(), Box> { // The iterator yields Result, so we check the // error here. let record = result?; - println!("{:?}", record); + + // Records can be indexed like a vector to return single strings + let cell: &str = &record[0]; + + println!("The first cell is {:?}", cell); } Ok(()) } From 38a876b8f0737c8d620be86dcf1fe00d93fb2437 Mon Sep 17 00:00:00 2001 From: Michael Milton Date: Mon, 16 Aug 2021 01:33:37 +1000 Subject: [PATCH 2/2] Update example file --- examples/cookbook-read-basic.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/examples/cookbook-read-basic.rs b/examples/cookbook-read-basic.rs index a69b0e2d..40f1b5f8 100644 --- a/examples/cookbook-read-basic.rs +++ b/examples/cookbook-read-basic.rs @@ -7,9 +7,13 @@ fn example() -> Result<(), Box> { let mut rdr = csv::Reader::from_reader(io::stdin()); for result in rdr.records() { // The iterator yields Result, so we check the - // error here.. + // error here. let record = result?; - println!("{:?}", record); + + // Records can be indexed like a vector to return single strings + let cell: &str = &record[0]; + + println!("The first cell is {:?}", cell); } Ok(()) }