Skip to content

Commit aee1de5

Browse files
committed
Clarifications.
1 parent 8e6d015 commit aee1de5

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

active/0000-error-conventions.md renamed to text/0000-error-conventions.md

+22-16
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ but it tries to motivate and clarify them.
4747

4848
Errors fall into one of three categories:
4949

50-
* Catastrophic errors, e.g. out of memory.
50+
* Catastrophic errors, e.g. out-of-memory.
5151
* Contract violations, e.g. wrong input encoding, index out of bounds.
5252
* Obstructions, e.g. file not found, parse error.
5353

@@ -67,14 +67,11 @@ Catastrophic errors are _extremely_ rare, especially outside of `libstd`.
6767

6868
**Canonical examples**: out of memory, stack overflow.
6969

70-
### For catastrophic errors, abort the process or fail the task.
70+
### For catastrophic errors, fail the task.
7171

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.
7875

7976
## Contract violations
8077

@@ -137,22 +134,31 @@ aspects of the input that are not covered by the contract.
137134

138135
**Canonical examples**: file not found, parse error.
139136

140-
### For obstructions, use `Result` (or `Option`).
137+
### For obstructions, use `Result`
141138

142139
The
143140
[`Result<T,E>` type](http://static.rust-lang.org/doc/master/std/result/index.html)
144141
represents either a success (yielding `T`) or failure (yielding `E`). By
145142
returning a `Result`, a function allows its clients to discover and react to
146143
obstructions in a fine-grained way.
147144

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.
153156

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.
156162

157163
## Do not provide both `Result` and `fail!` variants.
158164

0 commit comments

Comments
 (0)