diff options
| author | kgv <mail@kgv.name> | 2015-04-01 09:37:19 +0300 |
|---|---|---|
| committer | kgv <mail@kgv.name> | 2015-04-01 09:37:19 +0300 |
| commit | 343c110e76ddb673ed8bfc22ea917c3146c7b6e3 (patch) | |
| tree | bc85558cedac699800eab1146b732e2e4723656e | |
| parent | d754722a04b99fdcae0fd97fa2a4395521145ef2 (diff) | |
Fix rust book error-handling.md for new std::io.
Fix example and some text for: `read_line` takes `&mut String` and return `Result` instead `IoResult`.
| -rw-r--r-- | src/doc/trpl/error-handling.md | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/src/doc/trpl/error-handling.md b/src/doc/trpl/error-handling.md index cf60bd88c54..b9e7bd78c5b 100644 --- a/src/doc/trpl/error-handling.md +++ b/src/doc/trpl/error-handling.md @@ -200,15 +200,15 @@ Because these kinds of situations are relatively rare, use panics sparingly. # Upgrading failures to panics In certain circumstances, even though a function may fail, we may want to treat -it as a panic instead. For example, `io::stdin().read_line()` returns an -`IoResult<String>`, a form of `Result`, when there is an error reading the -line. This allows us to handle and possibly recover from this sort of error. +it as a panic instead. For example, `io::stdin().read_line(&mut buffer)` returns +an `Result<usize>`, when there is an error reading the line. This allows us to +handle and possibly recover from error. If we don't want to handle this error, and would rather just abort the program, we can use the `unwrap()` method: ```{rust,ignore} -io::stdin().read_line().unwrap(); +io::stdin().read_line(&mut buffer).unwrap(); ``` `unwrap()` will `panic!` if the `Option` is `None`. This basically says "Give @@ -219,12 +219,13 @@ shorter. Sometimes, just crashing is appropriate. There's another way of doing this that's a bit nicer than `unwrap()`: ```{rust,ignore} -let input = io::stdin().read_line() +let mut buffer = String::new(); +let input = io::stdin().read_line(&mut buffer) .ok() .expect("Failed to read line"); ``` -`ok()` converts the `IoResult` into an `Option`, and `expect()` does the same +`ok()` converts the `Result` into an `Option`, and `expect()` does the same thing as `unwrap()`, but takes a message. This message is passed along to the underlying `panic!`, providing a better error message if the code errors. |
