@@ -20,18 +20,18 @@ panic. A *failure* is an error that can be recovered from in some way. A
20
20
* panic* is an error that cannot be recovered from.
21
21
22
22
What 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:
24
24
25
- ``` {rust, ignore}
26
- from_str( "5");
25
+ ``` ignore
26
+ "5".parse( );
27
27
```
28
28
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?
32
32
33
- ``` {rust, ignore}
34
- from_str( "hello5world");
33
+ ``` ignore
34
+ "hello5world".parse( );
35
35
```
36
36
37
37
This 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*.
40
40
On the other hand, sometimes, there are errors that are unexpected, or which
41
41
we cannot recover from. A classic example is an ` assert! ` :
42
42
43
- ``` {rust,ignore}
43
+ ``` rust
44
+ # let x = 5 ;
44
45
assert! (x == 5 );
45
46
```
46
47
@@ -119,17 +120,19 @@ Rust calls these sorts of errors *panics*.
119
120
# Handling errors with ` Option ` and ` Result `
120
121
121
122
The 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 ` :
123
125
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 );
126
131
```
127
132
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 ` .
130
133
131
134
This 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
133
136
failed? For this, we can use the ` Result<T, E> ` type. It looks like this:
134
137
135
138
``` rust
@@ -297,5 +300,5 @@ It's worth noting that you can only use `try!` from a function that returns a
297
300
` Result ` , which means that you cannot use ` try! ` inside of ` main() ` , because
298
301
` main() ` doesn't return anything.
299
302
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
301
304
what to return in the error case.
0 commit comments