@@ -1795,6 +1795,10 @@ To convert this to proper error handling, we need to do the following:
17951795Let's try it:
17961796
17971797``` rust,ignore
1798+ use std::error::Error
1799+
1800+ // The rest of the code before this is unchanged
1801+
17981802fn search<P: AsRef<Path>>
17991803 (file_path: P, city: &str)
18001804 -> Result<Vec<PopulationCount>, Box<Error+Send+Sync>> {
@@ -1903,8 +1907,13 @@ let city = if !matches.free.is_empty() {
19031907 return;
19041908};
19051909
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)
19081917}
19091918...
19101919```
@@ -1927,6 +1936,10 @@ that it is generic on some type parameter `R` that satisfies
19271936` io::Read ` . Another way is to just use trait objects:
19281937
19291938``` rust,ignore
1939+ use std::io;
1940+
1941+ // The rest of the code before this is unchanged
1942+
19301943fn search<P: AsRef<Path>>
19311944 (file_path: &Option<P>, city: &str)
19321945 -> Result<Vec<PopulationCount>, Box<Error+Send+Sync>> {
0 commit comments