@@ -47,7 +47,7 @@ but it tries to motivate and clarify them.
47
47
48
48
Errors fall into one of three categories:
49
49
50
- * Catastrophic errors, e.g. out of memory.
50
+ * Catastrophic errors, e.g. out-of- memory.
51
51
* Contract violations, e.g. wrong input encoding, index out of bounds.
52
52
* Obstructions, e.g. file not found, parse error.
53
53
@@ -67,14 +67,11 @@ Catastrophic errors are _extremely_ rare, especially outside of `libstd`.
67
67
68
68
** Canonical examples** : out of memory, stack overflow.
69
69
70
- ### For catastrophic errors, abort the process or fail the task.
70
+ ### For catastrophic errors, fail the task.
71
71
72
- For errors like out-of-memory, even correctly unwinding is impossible (since it
73
- may require allocation); the process is aborted.
74
-
75
- For errors like stack overflow, Rust currently aborts the process, but could in
76
- principle fail the task, which would allow reporting and recovery from a
77
- supervisory task.
72
+ For errors like stack overflow, Rust currently aborts the process, but
73
+ could in principle fail the task, which (in the best case) would allow
74
+ reporting and recovery from a supervisory task.
78
75
79
76
## Contract violations
80
77
@@ -137,22 +134,31 @@ aspects of the input that are not covered by the contract.
137
134
138
135
** Canonical examples** : file not found, parse error.
139
136
140
- ### For obstructions, use ` Result ` (or ` Option ` ).
137
+ ### For obstructions, use ` Result `
141
138
142
139
The
143
140
[ ` Result<T,E> ` type] ( http://static.rust-lang.org/doc/master/std/result/index.html )
144
141
represents either a success (yielding ` T ` ) or failure (yielding ` E ` ). By
145
142
returning a ` Result ` , a function allows its clients to discover and react to
146
143
obstructions in a fine-grained way.
147
144
148
- If there are multiple ways an operation might be obstructed, or there is useful
149
- information about the obstruction (such as where in the input a parse error
150
- occurred), prefer to use ` Result ` . For operations with a single obvious
151
- obstruction that can provide no additional information (e.g. popping from an
152
- empty stack), use ` Option ` .
145
+ #### What about ` Option ` ?
146
+
147
+ The ` Option ` type should not be used for "obstructed" operations; it
148
+ should only be used when a ` None ` return value could be considered a
149
+ "successful" execution of the operation.
150
+
151
+ This is of course a somewhat subjective question, but a good litmus
152
+ test is: would a reasonable client ever ignore the result? The
153
+ ` Result ` type provides a lint that ensures the result is actually
154
+ inspected, while ` Option ` does not, and this difference of behavior
155
+ can help when deciding between the two types.
153
156
154
- (Currently, ` Option ` does not interact well with other error-handling
155
- infrastructure like ` try! ` , but this will likely be improved in the future.)
157
+ Another litmus test: can the operation be understood as asking a
158
+ question (possibly with sideeffects)? Operations like ` pop ` on a
159
+ vector can be viewed as asking for the contents of the first element,
160
+ with the side effect of removing it if it exists -- with an ` Option `
161
+ return value.
156
162
157
163
## Do not provide both ` Result ` and ` fail! ` variants.
158
164
0 commit comments