@@ -186,6 +186,7 @@ But wait, what about `unwrap` used in [`unwrap-double`](#code-unwrap-double)?
186
186
There was no case analysis there! Instead, the case analysis was put inside the
187
187
` unwrap ` method for you. You could define it yourself if you want:
188
188
189
+ <div id =" code-option-def-unwrap " >
189
190
``` rust
190
191
enum Option <T > {
191
192
None ,
@@ -202,6 +203,7 @@ impl<T> Option<T> {
202
203
}
203
204
}
204
205
```
206
+ </div >
205
207
206
208
The ` unwrap ` method * abstracts away the case analysis* . This is precisely the thing
207
209
that makes ` unwrap ` ergonomic to use. Unfortunately, that ` panic! ` means that
@@ -251,6 +253,7 @@ option is `None`, in which case, just return `None`.
251
253
Rust has parametric polymorphism, so it is very easy to define a combinator
252
254
that abstracts this pattern:
253
255
256
+ <div id =" code-option-map " >
254
257
``` rust
255
258
fn map <F , T , A >(option : Option <T >, f : F ) -> Option <A > where F : FnOnce (T ) -> A {
256
259
match option {
@@ -259,6 +262,7 @@ fn map<F, T, A>(option: Option<T>, f: F) -> Option<A> where F: FnOnce(T) -> A {
259
262
}
260
263
}
261
264
```
265
+ </div >
262
266
263
267
Indeed, ` map ` is [ defined as a method] [ 2 ] on ` Option<T> ` in the standard library.
264
268
@@ -390,12 +394,14 @@ remove choices because they will panic if `Option<T>` is `None`.
390
394
The ` Result ` type is also
391
395
[ defined in the standard library] [ 6 ] :
392
396
397
+ <div id =" code-result-def-1 " >
393
398
``` rust
394
399
enum Result <T , E > {
395
400
Ok (T ),
396
401
Err (E ),
397
402
}
398
403
```
404
+ </div >
399
405
400
406
The ` Result ` type is a richer version of ` Option ` . Instead of expressing the
401
407
possibility of * absence* like ` Option ` does, ` Result ` expresses the possibility
@@ -666,6 +672,7 @@ with both an `Option` and a `Result`, the solution is *usually* to convert the
666
672
(from ` env::args() ` ) means the user didn't invoke the program correctly. We
667
673
could just use a ` String ` to describe the error. Let's try:
668
674
675
+ <div id =" code-error-double-string " >
669
676
``` rust
670
677
use std :: env;
671
678
@@ -682,6 +689,7 @@ fn main() {
682
689
}
683
690
}
684
691
```
692
+ </div >
685
693
686
694
There are a couple new things in this example. The first is the use of the
687
695
[ ` Option::ok_or ` ] ( ../std/option/enum.Option.html#method.ok_or )
@@ -898,6 +906,7 @@ seen above.
898
906
899
907
Here is a simplified definition of a ` try! ` macro:
900
908
909
+ <div id =" code-try-def-simple " >
901
910
``` rust
902
911
macro_rules! try {
903
912
($ e : expr ) => (match $ e {
@@ -906,6 +915,7 @@ macro_rules! try {
906
915
});
907
916
}
908
917
```
918
+ </div >
909
919
910
920
(The [ real definition] ( ../std/macro.try!.html ) is a bit more
911
921
sophisticated. We will address that later.)
@@ -1158,11 +1168,13 @@ The `std::convert::From` trait is
1158
1168
[ defined in the standard
1159
1169
library] ( ../std/convert/trait.From.html ) :
1160
1170
1171
+ <div id =" code-from-def " >
1161
1172
``` rust
1162
1173
trait From <T > {
1163
1174
fn from (T ) -> Self ;
1164
1175
}
1165
1176
```
1177
+ </div >
1166
1178
1167
1179
Deliciously simple, yes? ` From ` is very useful because it gives us a generic
1168
1180
way to talk about conversion * from* a particular type ` T ` to some other type
@@ -1238,6 +1250,7 @@ macro_rules! try {
1238
1250
This is not it's real definition. It's real definition is
1239
1251
[ in the standard library] ( ../std/macro.try!.html ) :
1240
1252
1253
+ <div id =" code-try-def " >
1241
1254
``` rust
1242
1255
macro_rules! try {
1243
1256
($ e : expr ) => (match $ e {
@@ -1246,6 +1259,7 @@ macro_rules! try {
1246
1259
});
1247
1260
}
1248
1261
```
1262
+ </div >
1249
1263
1250
1264
There's one tiny but powerful change: the error value is passed through
1251
1265
` From::from ` . This makes the ` try! ` macro a lot more powerful because it gives
0 commit comments