Skip to content

Commit 291a10d

Browse files
est31brson
authored andcommitted
Document the question mark operator
1 parent d9c5089 commit 291a10d

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/doc/book/syntax-index.md

+2
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
* `|=` (`var |= expr`): bitwise or & assignment. Overloadable (`BitOrAssign`).
9595
* `||` (`expr || expr`): logical or.
9696
* `_`: "ignored" pattern binding (see [Patterns (Ignoring bindings)]). Also used to make integer-literals readable (see [Reference (Integer literals)]).
97+
* `?` (`expr?`): Error propagation. Returns early when `Err(_)` is encountered, unwraps otherwise. Similar to the [`try!` macro].
9798

9899
## Other Syntax
99100

@@ -210,6 +211,7 @@
210211
[Functions]: functions.html
211212
[Generics]: generics.html
212213
[Iterators]: iterators.html
214+
[`try!` macro]: error-handling.html#the-try-macro
213215
[Lifetimes]: lifetimes.html
214216
[Loops (`for`)]: loops.html#for
215217
[Loops (`loop`)]: loops.html#loop

src/doc/reference.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -2858,8 +2858,8 @@ assert_eq!(x, y);
28582858

28592859
### Unary operator expressions
28602860

2861-
Rust defines the following unary operators. They are all written as prefix operators,
2862-
before the expression they apply to.
2861+
Rust defines the following unary operators. With the exception of `?`, they are
2862+
all written as prefix operators, before the expression they apply to.
28632863

28642864
* `-`
28652865
: Negation. Signed integer types and floating-point types support negation. It
@@ -2888,6 +2888,10 @@ before the expression they apply to.
28882888
If the `&` or `&mut` operators are applied to an rvalue, a
28892889
temporary value is created; the lifetime of this temporary value
28902890
is defined by [syntactic rules](#temporary-lifetimes).
2891+
* `?`
2892+
: Propagating errors if applied to `Err(_)` and unwrapping if
2893+
applied to `Ok(_)`. Only works on the `Result<T, E>` type,
2894+
and written in postfix notation.
28912895

28922896
### Binary operator expressions
28932897

0 commit comments

Comments
 (0)