Skip to content

Commit 1e9aad7

Browse files
committed
Document the question mark operator
1 parent ecd79a1 commit 1e9aad7

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
@@ -2860,8 +2860,8 @@ assert_eq!(x, y);
28602860

28612861
### Unary operator expressions
28622862

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

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

28942898
### Binary operator expressions
28952899

0 commit comments

Comments
 (0)