@@ -28,7 +28,7 @@ systems may want to jump around.
28
28
* [ The ` Result ` type] ( #the-result-type )
29
29
* [ Parsing integers] ( #parsing-integers )
30
30
* [ The ` Result ` type alias idiom] ( #the-result-type-alias-idiom )
31
- * [ A brief interlude: unwrapping isn't evil] ( #a-brief-interlude-unwrapping-isnt -evil )
31
+ * [ A brief interlude: unwrapping isn't evil] ( #a-brief-interlude: -unwrapping-isn't -evil )
32
32
* [ Working with multiple error types] ( #working-with-multiple-error-types )
33
33
* [ Composing ` Option ` and ` Result ` ] ( #composing-option-and-result )
34
34
* [ The limits of combinators] ( #the-limits-of-combinators )
@@ -41,11 +41,11 @@ systems may want to jump around.
41
41
* [ The real ` try! ` macro] ( #the-real-try!-macro )
42
42
* [ Composing custom error types] ( #composing-custom-error-types )
43
43
* [ Advice for library writers] ( #advice-for-library-writers )
44
- * [ Case study: A program to read population data] ( #case-study-a-program-to-read-population-data )
44
+ * [ Case study: A program to read population data] ( #case-study: -a-program-to-read-population-data )
45
45
* [ Initial setup] ( #initial-setup )
46
46
* [ Argument parsing] ( #argument-parsing )
47
47
* [ Writing the logic] ( #writing-the-logic )
48
- * [ Error handling with ` Box<Error> ` ] ( #error-handling-with-box<error> )
48
+ * [ Error handling with ` Box<Error> ` ] ( #error-handling-with-box%3Cerror%3E )
49
49
* [ Reading from stdin] ( #reading-from-stdin )
50
50
* [ Error handling with a custom type] ( #error-handling-with-a-custom-type )
51
51
* [ Adding functionality] ( #adding-functionality )
@@ -87,9 +87,9 @@ thread '<main>' panicked at 'Invalid number: 11', src/bin/panic-simple.rs:5
87
87
Here's another example that is slightly less contrived. A program that accepts
88
88
an integer as an argument, doubles it and prints it.
89
89
90
- <div id =" code-unwrap-double " >
91
- ``` rust,should_panic
90
+ <a name =" code-unwrap-double " ></a >
92
91
92
+ ``` rust,should_panic
93
93
use std::env;
94
94
95
95
fn main() {
@@ -99,7 +99,6 @@ fn main() {
99
99
println!("{}", 2 * n);
100
100
}
101
101
```
102
- </div >
103
102
104
103
If you give this program zero arguments (error 1) or if the first argument
105
104
isn't an integer (error 2), the program will panic just like in the first
@@ -140,7 +139,8 @@ system is an important concept because it will cause the compiler to force the
140
139
programmer to handle that absence. Let's take a look at an example that tries
141
140
to find a character in a string:
142
141
143
- <div id =" code-option-ex-string-find " >
142
+ <a name =" code-option-ex-string-find " ></a >
143
+
144
144
``` rust
145
145
// Searches `haystack` for the Unicode character `needle`. If one is found, the
146
146
// byte offset of the character is returned. Otherwise, `None` is returned.
@@ -153,7 +153,6 @@ fn find(haystack: &str, needle: char) -> Option<usize> {
153
153
None
154
154
}
155
155
```
156
- </div >
157
156
158
157
Notice that when this function finds a matching character, it doen't just
159
158
return the ` offset ` . Instead, it returns ` Some(offset) ` . ` Some ` is a variant or
@@ -187,6 +186,8 @@ But wait, what about `unwrap` used in [`unwrap-double`](#code-unwrap-double)?
187
186
There was no case analysis there! Instead, the case analysis was put inside the
188
187
` unwrap ` method for you. You could define it yourself if you want:
189
188
189
+ <a name =" code-option-def-unwrap " ></a >
190
+
190
191
``` rust
191
192
enum Option <T > {
192
193
None ,
@@ -210,7 +211,7 @@ that makes `unwrap` ergonomic to use. Unfortunately, that `panic!` means that
210
211
211
212
### Composing ` Option<T> ` values
212
213
213
- In [ ` option-ex-string-find ` ] ( #code-option-ex-string-find-2 )
214
+ In [ ` option-ex-string-find ` ] ( #code-option-ex-string-find )
214
215
we saw how to use ` find ` to discover the extension in a file name. Of course,
215
216
not all file names have a ` . ` in them, so it's possible that the file name has
216
217
no extension. This * possibility of absence* is encoded into the types using
@@ -252,6 +253,8 @@ option is `None`, in which case, just return `None`.
252
253
Rust has parametric polymorphism, so it is very easy to define a combinator
253
254
that abstracts this pattern:
254
255
256
+ <a name =" code-option-map " ></a >
257
+
255
258
``` rust
256
259
fn map <F , T , A >(option : Option <T >, f : F ) -> Option <A > where F : FnOnce (T ) -> A {
257
260
match option {
@@ -391,6 +394,8 @@ remove choices because they will panic if `Option<T>` is `None`.
391
394
The ` Result ` type is also
392
395
[ defined in the standard library] [ 6 ] :
393
396
397
+ <a name =" code-result-def-1 " ></a >
398
+
394
399
``` rust
395
400
enum Result <T , E > {
396
401
Ok (T ),
@@ -667,6 +672,8 @@ with both an `Option` and a `Result`, the solution is *usually* to convert the
667
672
(from ` env::args() ` ) means the user didn't invoke the program correctly. We
668
673
could just use a ` String ` to describe the error. Let's try:
669
674
675
+ <a name =" code-error-double-string " ></a >
676
+
670
677
``` rust
671
678
use std :: env;
672
679
@@ -899,6 +906,8 @@ seen above.
899
906
900
907
Here is a simplified definition of a ` try! ` macro:
901
908
909
+ <a nama name =" code-try-def-simple " ></a >
910
+
902
911
``` rust
903
912
macro_rules! try {
904
913
($ e : expr ) => (match $ e {
@@ -1159,6 +1168,8 @@ The `std::convert::From` trait is
1159
1168
[ defined in the standard
1160
1169
library] ( ../std/convert/trait.From.html ) :
1161
1170
1171
+ <a name =" code-from-def " ></a >
1172
+
1162
1173
``` rust
1163
1174
trait From <T > {
1164
1175
fn from (T ) -> Self ;
@@ -1236,9 +1247,11 @@ macro_rules! try {
1236
1247
}
1237
1248
```
1238
1249
1239
- This is not it's real definition. It's real definition is
1250
+ This is not its real definition. Its real definition is
1240
1251
[ in the standard library] ( ../std/macro.try!.html ) :
1241
1252
1253
+ <a name =" code-try-def " ></a >
1254
+
1242
1255
``` rust
1243
1256
macro_rules! try {
1244
1257
($ e : expr ) => (match $ e {
@@ -1457,7 +1470,7 @@ representation. But certainly, this will vary depending on use cases.
1457
1470
At a minimum, you should probably implement the
1458
1471
[ ` Error ` ] ( ../std/error/trait.Error.html )
1459
1472
trait. This will give users of your library some minimum flexibility for
1460
- [ composing errors] ( #the-real-try-macro ) . Implementing the ` Error ` trait also
1473
+ [ composing errors] ( #the-real-try! -macro ) . Implementing the ` Error ` trait also
1461
1474
means that users are guaranteed the ability to obtain a string representation
1462
1475
of an error (because it requires impls for both ` fmt::Debug ` and
1463
1476
` fmt::Display ` ).
0 commit comments