@@ -1613,6 +1613,9 @@ CSV data given to us and print out a field in matching rows. Let's do it. (Make
1613
1613
sure to add ` extern crate csv; ` to the top of your file.)
1614
1614
1615
1615
``` rust,ignore
1616
+ use std::fs::File;
1617
+ use std::path::Path;
1618
+
1616
1619
// This struct represents the data in each row of the CSV file.
1617
1620
// Type based decoding absolves us of a lot of the nitty gritty error
1618
1621
// handling, like parsing strings as integers or floats.
@@ -1656,7 +1659,7 @@ fn main() {
1656
1659
let data_path = Path::new(&data_file);
1657
1660
let city = args[2].clone();
1658
1661
1659
- let file = fs:: File::open(data_path).unwrap();
1662
+ let file = File::open(data_path).unwrap();
1660
1663
let mut rdr = csv::Reader::from_reader(file);
1661
1664
1662
1665
for row in rdr.decode::<Row>() {
@@ -1674,7 +1677,7 @@ fn main() {
1674
1677
Let's outline the errors. We can start with the obvious: the three places that
1675
1678
` unwrap ` is called:
1676
1679
1677
- 1 . [ ` fs:: File::open` ] ( ../std/fs/struct.File.html#method.open )
1680
+ 1 . [ ` File::open ` ] ( ../std/fs/struct.File.html#method.open )
1678
1681
can return an
1679
1682
[ ` io::Error ` ] ( ../std/io/struct.Error.html ) .
1680
1683
2 . [ ` csv::Reader::decode ` ] ( http://burntsushi.net/rustdoc/csv/struct.Reader.html#method.decode )
@@ -1734,7 +1737,7 @@ fn print_usage(program: &str, opts: Options) {
1734
1737
1735
1738
fn search<P: AsRef<Path>>(file_path: P, city: &str) -> Vec<PopulationCount> {
1736
1739
let mut found = vec![];
1737
- let file = fs:: File::open(file_path).unwrap();
1740
+ let file = File::open(file_path).unwrap();
1738
1741
let mut rdr = csv::Reader::from_reader(file);
1739
1742
for row in rdr.decode::<Row>() {
1740
1743
let row = row.unwrap();
@@ -1796,7 +1799,7 @@ fn search<P: AsRef<Path>>
1796
1799
(file_path: P, city: &str)
1797
1800
-> Result<Vec<PopulationCount>, Box<Error+Send+Sync>> {
1798
1801
let mut found = vec![];
1799
- let file = try!(fs:: File::open(file_path));
1802
+ let file = try!(File::open(file_path));
1800
1803
let mut rdr = csv::Reader::from_reader(file);
1801
1804
for row in rdr.decode::<Row>() {
1802
1805
let row = try!(row);
@@ -1930,7 +1933,7 @@ fn search<P: AsRef<Path>>
1930
1933
let mut found = vec![];
1931
1934
let input: Box<io::Read> = match *file_path {
1932
1935
None => Box::new(io::stdin()),
1933
- Some(ref file_path) => Box::new(try!(fs:: File::open(file_path))),
1936
+ Some(ref file_path) => Box::new(try!(File::open(file_path))),
1934
1937
};
1935
1938
let mut rdr = csv::Reader::from_reader(input);
1936
1939
// The rest remains unchanged!
@@ -2017,7 +2020,7 @@ fn search<P: AsRef<Path>>
2017
2020
let mut found = vec![];
2018
2021
let input: Box<io::Read> = match *file_path {
2019
2022
None => Box::new(io::stdin()),
2020
- Some(ref file_path) => Box::new(try!(fs:: File::open(file_path))),
2023
+ Some(ref file_path) => Box::new(try!(File::open(file_path))),
2021
2024
};
2022
2025
let mut rdr = csv::Reader::from_reader(input);
2023
2026
for row in rdr.decode::<Row>() {
0 commit comments