Skip to content

Commit a270aee

Browse files
authored
Rollup merge of #103644 - catlee:catlee/option-question-mark-docs, r=workingjubilee
Add docs for question mark operator for Option As a beginner learning rust, it took me a while to figure out what `?` was doing with Options. I think the documentation of this could be improved. I've used the question mark documentation from the `Result` type as a template here, and tried to come up with a simple example as well.
2 parents 309c469 + e0fd37d commit a270aee

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

library/core/src/option.rs

+44
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,50 @@
7272
//! }
7373
//! ```
7474
//!
75+
//! # The question mark operator, `?`
76+
//!
77+
//! Similar to the [`Result`] type, when writing code that calls many functions that return the
78+
//! [`Option`] type, handling `Some`/`None` can be tedious. The question mark
79+
//! operator, [`?`], hides some of the boilerplate of propagating values
80+
//! up the call stack.
81+
//!
82+
//! It replaces this:
83+
//!
84+
//! ```
85+
//! # #![allow(dead_code)]
86+
//! fn add_last_numbers(stack: &mut Vec<i32>) -> Option<i32> {
87+
//! let a = stack.pop();
88+
//! let b = stack.pop();
89+
//!
90+
//! match (a, b) {
91+
//! (Some(x), Some(y)) => Some(x + y),
92+
//! _ => None,
93+
//! }
94+
//! }
95+
//!
96+
//! ```
97+
//!
98+
//! With this:
99+
//!
100+
//! ```
101+
//! # #![allow(dead_code)]
102+
//! fn add_last_numbers(stack: &mut Vec<i32>) -> Option<i32> {
103+
//! Some(stack.pop()? + stack.pop()?)
104+
//! }
105+
//! ```
106+
//!
107+
//! *It's much nicer!*
108+
//!
109+
//! Ending the expression with [`?`] will result in the [`Some`]'s unwrapped value, unless the
110+
//! result is [`None`], in which case [`None`] is returned early from the enclosing function.
111+
//!
112+
//! [`?`] can be used in functions that return [`Option`] because of the
113+
//! early return of [`None`] that it provides.
114+
//!
115+
//! [`?`]: crate::ops::Try
116+
//! [`Some`]: Some
117+
//! [`None`]: None
118+
//!
75119
//! # Representation
76120
//!
77121
//! Rust guarantees to optimize the following types `T` such that

library/core/src/result.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,10 @@
209209
//!
210210
//! *It's much nicer!*
211211
//!
212-
//! Ending the expression with [`?`] will result in the unwrapped
213-
//! success ([`Ok`]) value, unless the result is [`Err`], in which case
214-
//! [`Err`] is returned early from the enclosing function.
212+
//! Ending the expression with [`?`] will result in the [`Ok`]'s unwrapped value, unless the result
213+
//! is [`Err`], in which case [`Err`] is returned early from the enclosing function.
215214
//!
216-
//! [`?`] can only be used in functions that return [`Result`] because of the
215+
//! [`?`] can be used in functions that return [`Result`] because of the
217216
//! early return of [`Err`] that it provides.
218217
//!
219218
//! [`expect`]: Result::expect

0 commit comments

Comments
 (0)