@@ -1795,6 +1795,10 @@ To convert this to proper error handling, we need to do the following:
1795
1795
Let's try it:
1796
1796
1797
1797
``` rust,ignore
1798
+ use std::error::Error
1799
+
1800
+ // The rest of the code before this is unchanged
1801
+
1798
1802
fn search<P: AsRef<Path>>
1799
1803
(file_path: P, city: &str)
1800
1804
-> Result<Vec<PopulationCount>, Box<Error+Send+Sync>> {
@@ -1903,8 +1907,13 @@ let city = if !matches.free.is_empty() {
1903
1907
return;
1904
1908
};
1905
1909
1906
- for pop in search(&data_file, &city) {
1907
- println!("{}, {}: {:?}", pop.city, pop.country, pop.count);
1910
+ match search(&data_file, &city) {
1911
+ Ok(pops) => {
1912
+ for pop in pops {
1913
+ println!("{}, {}: {:?}", pop.city, pop.country, pop.count);
1914
+ }
1915
+ }
1916
+ Err(err) => println!("{}", err)
1908
1917
}
1909
1918
...
1910
1919
```
@@ -1927,6 +1936,10 @@ that it is generic on some type parameter `R` that satisfies
1927
1936
` io::Read ` . Another way is to just use trait objects:
1928
1937
1929
1938
``` rust,ignore
1939
+ use std::io;
1940
+
1941
+ // The rest of the code before this is unchanged
1942
+
1930
1943
fn search<P: AsRef<Path>>
1931
1944
(file_path: &Option<P>, city: &str)
1932
1945
-> Result<Vec<PopulationCount>, Box<Error+Send+Sync>> {
0 commit comments