Skip to content

Commit 9eb099d

Browse files
committed
Auto merge of #29686 - jrburke:docs-error-handling-search-case, r=BurntSushi
In src/doc/trpl/error-handling.md, in this section: It mentions three steps, to "convert this to proper error handling", the last one being: 3. Handle the error in main. However, it is not shown. This pull request adds a code example showing how `main`'s call to `search` should use case analysis. I am still very much new to learning Rust, so this may not be idiomatic. Happy to make changes with guidance.
2 parents e543815 + 0dd2c1c commit 9eb099d

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/doc/trpl/error-handling.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1838,6 +1838,22 @@ impl<'a, 'b> From<&'b str> for Box<Error + Send + Sync + 'a>
18381838
impl From<String> for Box<Error + Send + Sync>
18391839
```
18401840

1841+
Since `search` now returns a `Result<T, E>`, `main` should use case analysis
1842+
when calling `search`:
1843+
1844+
```rust,ignore
1845+
...
1846+
match search(&data_file, &city) {
1847+
Ok(pops) => {
1848+
for pop in pops {
1849+
println!("{}, {}: {:?}", pop.city, pop.country, pop.count);
1850+
}
1851+
}
1852+
Err(err) => println!("{}", err)
1853+
}
1854+
...
1855+
```
1856+
18411857
Now that we've seen how to do proper error handling with `Box<Error>`, let's
18421858
try a different approach with our own custom error type. But first, let's take
18431859
a quick break from error handling and add support for reading from `stdin`.

0 commit comments

Comments
 (0)