@@ -20,18 +20,18 @@ panic. A *failure* is an error that can be recovered from in some way. A
2020* panic* is an error that cannot be recovered from.
2121
2222What do we mean by "recover"? Well, in most cases, the possibility of an error
23- is expected. For example, consider the ` from_str ` function:
23+ is expected. For example, consider the ` parse ` function:
2424
25- ``` {rust, ignore}
26- from_str( "5");
25+ ``` ignore
26+ "5".parse( );
2727```
2828
29- This function takes a string argument and converts it into another type. But
30- because it's a string, you can't be sure that the conversion actually works.
31- For example, what should this convert to?
29+ This method converts a string into another type. But because it's a string, you
30+ can't be sure that the conversion actually works. For example, what should this
31+ convert to?
3232
33- ``` {rust, ignore}
34- from_str( "hello5world");
33+ ``` ignore
34+ "hello5world".parse( );
3535```
3636
3737This won't work. So we know that this function will only work properly for some
@@ -40,7 +40,8 @@ inputs. It's expected behavior. We call this kind of error a *failure*.
4040On the other hand, sometimes, there are errors that are unexpected, or which
4141we cannot recover from. A classic example is an ` assert! ` :
4242
43- ``` {rust,ignore}
43+ ``` rust
44+ # let x = 5 ;
4445assert! (x == 5 );
4546```
4647
@@ -119,17 +120,19 @@ Rust calls these sorts of errors *panics*.
119120# Handling errors with ` Option ` and ` Result `
120121
121122The simplest way to indicate that a function may fail is to use the ` Option<T> `
122- type. Remember our ` from_str() ` example? Here's its type signature:
123+ type. For example, the ` find ` method on strings attempts to find a pattern
124+ in a string, and returns an ` Option ` :
123125
124- ``` {rust,ignore}
125- pub fn from_str<A: FromStr>(s: &str) -> Option<A>
126+ ``` rust
127+ let s = " foo" ;
128+
129+ assert_eq! (s . find ('f' ), Some (0 ));
130+ assert_eq! (s . find ('z' ), None );
126131```
127132
128- ` from_str() ` returns an ` Option<A> ` . If the conversion succeeds, it will return
129- ` Some(value) ` , and if it fails, it will return ` None ` .
130133
131134This is appropriate for the simplest of cases, but doesn't give us a lot of
132- information in the failure case. What if we wanted to know _ why_ the conversion
135+ information in the failure case. What if we wanted to know _ why_ the function
133136failed? For this, we can use the ` Result<T, E> ` type. It looks like this:
134137
135138``` rust
@@ -297,5 +300,5 @@ It's worth noting that you can only use `try!` from a function that returns a
297300` Result ` , which means that you cannot use ` try! ` inside of ` main() ` , because
298301` main() ` doesn't return anything.
299302
300- ` try! ` makes use of [ ` From<Error> ` ] ( ../std/convert/trait.From.hml ) to determine
303+ ` try! ` makes use of [ ` From<Error> ` ] ( ../std/convert/trait.From.html ) to determine
301304what to return in the error case.
0 commit comments