Skip to content

Commit 4b9e929

Browse files
committed
option/result overviews: address feedback
(Most of these are from a review by joshtriplett. Thanks!) Fix errors in `as_pin_ref` and `as_pin_mut` in the "Adapters for working with references" overview. Reword some headings about transformation methods. Reclassify `map`, `map_or`, `map_or_else`, `map_err`, etc. to more accurately reflect which variants they transform. Document `Debug` requirement for `get_or_insert_default`. Reword text about `take` and `replace` to be more accurate. Add examples for the `Product` and `Sum` traits. Also: Move link reference definitions closer to their uses. Warn about making link reference definintions for `err` and `ok`. Avoid making other link reference definitions that might conflict in the future (foreign methods that share a name with local ones, etc.) Write out the generics of `Option` and `Result` when the following text refers to the type parameters.
1 parent 9374be1 commit 4b9e929

File tree

2 files changed

+160
-124
lines changed

2 files changed

+160
-124
lines changed

Diff for: core/src/option.rs

+88-70
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
//! no "null" references. Instead, Rust has *optional* pointers, like
5050
//! the optional owned box, [`Option`]`<`[`Box<T>`]`>`.
5151
//!
52+
//! [`Box<T>`]: ../../std/boxed/struct.Box.html
53+
//!
5254
//! The following example uses [`Option`] to create an optional box of
5355
//! [`i32`]. Notice that in order to use the inner [`i32`] value, the
5456
//! `check_optional` function first needs to use pattern matching to
@@ -83,6 +85,10 @@
8385
//! * [`ptr::NonNull<U>`]
8486
//! * `#[repr(transparent)]` struct around one of the types in this list.
8587
//!
88+
//! [`Box<U>`]: ../../std/boxed/struct.Box.html
89+
//! [`num::NonZero*`]: crate::num
90+
//! [`ptr::NonNull<U>`]: crate::ptr::NonNull
91+
//!
8692
//! This is called the "null pointer optimization" or NPO.
8793
//!
8894
//! It is further guaranteed that, for the cases above, one can
@@ -100,32 +106,32 @@
100106
//! The [`is_some`] and [`is_none`] methods return [`true`] if the [`Option`]
101107
//! is [`Some`] or [`None`], respectively.
102108
//!
103-
//! [`is_some`]: Option::is_some
104109
//! [`is_none`]: Option::is_none
110+
//! [`is_some`]: Option::is_some
105111
//!
106112
//! ## Adapters for working with references
107113
//!
108114
//! * [`as_ref`] converts from `&Option<T>` to `Option<&T>`
109115
//! * [`as_mut`] converts from `&mut Option<T>` to `Option<&mut T>`
110116
//! * [`as_deref`] converts from `&Option<T>` to `Option<&T::Target>`
111-
//! * [`as_deref_mut`] converts from `&mut Option<T>` to `Option<&mut T::Target>`
112-
//! * [`as_pin_ref`] converts from [`&Pin`]`<Option<T>>` to `Option<`[`Pin`]`<&T>>`
113-
//! * [`as_pin_mut`] converts from [`&mut Pin`]`<Option<T>>` to `Option<`[`Pin`]`<&mut T>>`
117+
//! * [`as_deref_mut`] converts from `&mut Option<T>` to
118+
//! `Option<&mut T::Target>`
119+
//! * [`as_pin_ref`] converts from [`Pin`]`<&Option<T>>` to
120+
//! `Option<`[`Pin`]`<&T>>`
121+
//! * [`as_pin_mut`] converts from [`Pin`]`<&mut Option<T>>` to
122+
//! `Option<`[`Pin`]`<&mut T>>`
114123
//!
115-
//! [`&mut Pin`]: crate::pin::Pin
116-
//! [`&Pin`]: crate::pin::Pin
117124
//! [`as_deref`]: Option::as_deref
118125
//! [`as_deref_mut`]: Option::as_deref_mut
119126
//! [`as_mut`]: Option::as_mut
120-
//! [`as_pin_ref`]: Option::as_pin_ref
121127
//! [`as_pin_mut`]: Option::as_pin_mut
128+
//! [`as_pin_ref`]: Option::as_pin_ref
122129
//! [`as_ref`]: Option::as_ref
123-
//! [`Pin`]: crate::pin::Pin
124130
//!
125131
//! ## Extracting the contained value
126132
//!
127-
//! These methods extract the contained value in an [`Option`] when it is
128-
//! the [`Some`] variant. If the [`Option`] is [`None`]:
133+
//! These methods extract the contained value in an [`Option<T>`] when it
134+
//! is the [`Some`] variant. If the [`Option`] is [`None`]:
129135
//!
130136
//! * [`expect`] panics with a provided custom message
131137
//! * [`unwrap`] panics with a generic message
@@ -135,7 +141,6 @@
135141
//! * [`unwrap_or_else`] returns the result of evaluating the provided
136142
//! function
137143
//!
138-
//! [`Default`]: crate::default::Default
139144
//! [`expect`]: Option::expect
140145
//! [`unwrap`]: Option::unwrap
141146
//! [`unwrap_or`]: Option::unwrap_or
@@ -144,7 +149,7 @@
144149
//!
145150
//! ## Transforming contained values
146151
//!
147-
//! These transformations are from [`Option`] to [`Result`].
152+
//! These methods transform [`Option`] to [`Result`]:
148153
//!
149154
//! * [`ok_or`] transforms [`Some(v)`] to [`Ok(v)`], and [`None`] to
150155
//! [`Err(err)`] using the provided default `err` value
@@ -153,7 +158,14 @@
153158
//! * [`transpose`] transposes an [`Option`] of a [`Result`] into a
154159
//! [`Result`] of an [`Option`]
155160
//!
156-
//! These transformations are on [`Some`] values.
161+
//! [`Err(err)`]: Err
162+
//! [`Ok(v)`]: Ok
163+
//! [`Some(v)`]: Some
164+
//! [`ok_or`]: Option::ok_or
165+
//! [`ok_or_else`]: Option::ok_or_else
166+
//! [`transpose`]: Option::transpose
167+
//!
168+
//! These methods transform the [`Some`] variant:
157169
//!
158170
//! * [`filter`] calls the provided predicate function on the contained
159171
//! value `t` if the [`Option`] is [`Some(t)`], and returns [`Some(t)`]
@@ -163,38 +175,37 @@
163175
//! * [`map`] transforms [`Option<T>`] to [`Option<U>`] by applying the
164176
//! provided function to the contained value of [`Some`] and leaving
165177
//! [`None`] values unchanged
166-
//! * [`map_or`] transforms [`Option<T>`] to a value of `U` by applying the
167-
//! provided function to the contained value of [`Some`], or transforms
168-
//! [`None`] to a provided default value of `U`
169-
//! * [`map_or_else`] transforms [`Option<T>`] to a value of `U` by
170-
//! applying the provided function to the contained value of [`Some`], or
171-
//! transforms [`None`] to a value of `U` using a provided fallback
172-
//! function
173178
//!
174-
//! These transformations combine two [`Some`] values.
179+
//! [`Some(t)`]: Some
180+
//! [`filter`]: Option::filter
181+
//! [`flatten`]: Option::flatten
182+
//! [`map`]: Option::map
183+
//!
184+
//! These methods transform [`Option<T>`] to a value of a possibly
185+
//! different type `U`:
186+
//!
187+
//! * [`map_or`] applies the provided function to the contained value of
188+
//! [`Some`], or returns the provided default value if the [`Option`] is
189+
//! [`None`]
190+
//! * [`map_or_else`] applies the provided function to the contained value
191+
//! of [`Some`], or returns the result of evaluating the provided
192+
//! fallback function if the [`Option`] is [`None`]
193+
//!
194+
//! [`map_or`]: Option::map_or
195+
//! [`map_or_else`]: Option::map_or_else
196+
//!
197+
//! These methods combine the [`Some`] variants of two [`Option`] values:
175198
//!
176199
//! * [`zip`] returns [`Some((s, o))`] if `self` is [`Some(s)`] and the
177200
//! provided [`Option`] value is [`Some(o)`]; otherwise, returns [`None`]
178201
//! * [`zip_with`] calls the provided function `f` and returns
179202
//! [`Some(f(s, o))`] if `self` is [`Some(s)`] and the provided
180203
//! [`Option`] value is [`Some(o)`]; otherwise, returns [`None`]
181204
//!
182-
//! [`Err(err)`]: Err
183-
//! [`filter`]: Option::filter
184-
//! [`flatten`]: Option::flatten
185-
//! [`map`]: Option::map
186-
//! [`map_or`]: Option::map_or
187-
//! [`map_or_else`]: Option::map_or_else
188-
//! [`Ok(v)`]: Ok
189-
//! [`ok_or`]: Option::ok_or
190-
//! [`ok_or_else`]: Option::ok_or_else
191205
//! [`Some(f(s, o))`]: Some
192206
//! [`Some(o)`]: Some
193207
//! [`Some(s)`]: Some
194208
//! [`Some((s, o))`]: Some
195-
//! [`Some(t)`]: Some
196-
//! [`Some(v)`]: Some
197-
//! [`transpose`]: Option::transpose
198209
//! [`zip`]: Option::zip
199210
//! [`zip_with`]: Option::zip_with
200211
//!
@@ -223,6 +234,10 @@
223234
//! | [`xor`] | `Some(x)` | `None` | `Some(x)` |
224235
//! | [`xor`] | `Some(x)` | `Some(y)` | `None` |
225236
//!
237+
//! [`and`]: Option::and
238+
//! [`or`]: Option::or
239+
//! [`xor`]: Option::xor
240+
//!
226241
//! The [`and_then`] and [`or_else`] methods take a function as input, and
227242
//! only evaluate the function when they need to produce a new value. Only
228243
//! the [`and_then`] method can produce an [`Option<U>`] value having a
@@ -237,11 +252,8 @@
237252
//! | [`or_else`] | `None` | (not provided) | `Some(y)` | `Some(y)` |
238253
//! | [`or_else`] | `Some(x)` | (not provided) | (not evaluated) | `Some(x)` |
239254
//!
240-
//! [`and`]: Option::and
241255
//! [`and_then`]: Option::and_then
242-
//! [`or`]: Option::or
243256
//! [`or_else`]: Option::or_else
244-
//! [`xor`]: Option::xor
245257
//!
246258
//! This is an example of using methods like [`and_then`] and [`or`] in a
247259
//! pipeline of method calls. Early stages of the pipeline pass failure
@@ -282,7 +294,11 @@
282294
//! [`once(v)`] if the [`Option`] is [`Some(v)`], and like [`empty()`] if
283295
//! the [`Option`] is [`None`].
284296
//!
285-
//! Iterators over [`Option`] come in three types:
297+
//! [`Some(v)`]: Some
298+
//! [`empty()`]: crate::iter::empty
299+
//! [`once(v)`]: crate::iter::once
300+
//!
301+
//! Iterators over [`Option<T>`] come in three types:
286302
//!
287303
//! * [`into_iter`] consumes the [`Option`] and produces the contained
288304
//! value
@@ -291,12 +307,9 @@
291307
//! * [`iter_mut`] produces a mutable reference of type `&mut T` to the
292308
//! contained value
293309
//!
294-
//! [`empty()`]: crate::iter::empty
295310
//! [`into_iter`]: Option::into_iter
296311
//! [`iter`]: Option::iter
297312
//! [`iter_mut`]: Option::iter_mut
298-
//! [`once(v)`]: crate::iter::once
299-
//! [`Some(v)`]: Some
300313
//!
301314
//! An iterator over [`Option`] can be useful when chaining iterators, for
302315
//! example, to conditionally insert items. (It's not always necessary to
@@ -334,6 +347,9 @@
334347
//! we can't return `impl Iterator` anymore because the concrete types of
335348
//! the return values differ.
336349
//!
350+
//! [`empty()`]: crate::iter::empty
351+
//! [`once()`]: crate::iter::once
352+
//!
337353
//! ```compile_fail,E0308
338354
//! # use std::iter::{empty, once};
339355
//! // This won't compile because all possible returns from the function
@@ -347,16 +363,14 @@
347363
//! }
348364
//! ```
349365
//!
350-
//! [`once()`]: crate::iter::once
351-
//!
352366
//! ## Collecting into `Option`
353367
//!
354-
//! [`Option`] implements the [`FromIterator`] trait, which allows an
355-
//! iterator over [`Option`] values to be collected into an [`Option`] of a
356-
//! collection of each contained value of the original [`Option`] values,
357-
//! or [`None`] if any of the elements was [`None`].
368+
//! [`Option`] implements the [`FromIterator`][impl-FromIterator] trait,
369+
//! which allows an iterator over [`Option`] values to be collected into an
370+
//! [`Option`] of a collection of each contained value of the original
371+
//! [`Option`] values, or [`None`] if any of the elements was [`None`].
358372
//!
359-
//! [`FromIterator`]: Option#impl-FromIterator%3COption%3CA%3E%3E
373+
//! [impl-FromIterator]: Option#impl-FromIterator%3COption%3CA%3E%3E
360374
//!
361375
//! ```
362376
//! let v = vec![Some(2), Some(4), None, Some(8)];
@@ -367,43 +381,52 @@
367381
//! assert_eq!(res, Some(vec![2, 4, 8]));
368382
//! ```
369383
//!
370-
//! [`Option`] also implements the [`Product`] and [`Sum`] traits, allowing
371-
//! an iterator over [`Option`] values to provide the
372-
//! [`product`][m.product] and [`sum`][m.sum] methods.
384+
//! [`Option`] also implements the [`Product`][impl-Product] and
385+
//! [`Sum`][impl-Sum] traits, allowing an iterator over [`Option`] values
386+
//! to provide the [`product`][Iterator::product] and
387+
//! [`sum`][Iterator::sum] methods.
373388
//!
374-
//! [`Product`]: Option#impl-Product%3COption%3CU%3E%3E
375-
//! [`Sum`]: Option#impl-Sum%3COption%3CU%3E%3E
376-
//! [m.product]: crate::iter::Iterator::product
377-
//! [m.sum]: crate::iter::Iterator::sum
389+
//! [impl-Product]: Option#impl-Product%3COption%3CU%3E%3E
390+
//! [impl-Sum]: Option#impl-Sum%3COption%3CU%3E%3E
391+
//!
392+
//! ```
393+
//! let v = vec![None, Some(1), Some(2), Some(3)];
394+
//! let res: Option<i32> = v.into_iter().sum();
395+
//! assert_eq!(res, None);
396+
//! let v = vec![Some(1), Some(2), Some(21)];
397+
//! let res: Option<i32> = v.into_iter().product();
398+
//! assert_eq!(res, Some(42));
399+
//! ```
378400
//!
379401
//! ## Modifying an [`Option`] in-place
380402
//!
381-
//! These methods return a mutable reference to the contained value of a
382-
//! [`Some`].
403+
//! These methods return a mutable reference to the contained value of an
404+
//! [`Option<T>`]:
383405
//!
384406
//! * [`insert`] inserts a value, dropping any old contents
385407
//! * [`get_or_insert`] gets the current value, inserting a provided
386408
//! default value if it is [`None`]
387409
//! * [`get_or_insert_default`] gets the current value, inserting the
388-
//! default value of type `T` if it is [`None`]
410+
//! default value of type `T` (which must implement [`Default`]) if it is
411+
//! [`None`]
389412
//! * [`get_or_insert_with`] gets the current value, inserting a default
390413
//! computed by the provided function if it is [`None`]
391414
//!
392-
//! [`insert`]: Option::insert
393415
//! [`get_or_insert`]: Option::get_or_insert
394416
//! [`get_or_insert_default`]: Option::get_or_insert_default
395417
//! [`get_or_insert_with`]: Option::get_or_insert_with
418+
//! [`insert`]: Option::insert
396419
//!
397-
//! These methods transfer ownership of the [`Option`].
420+
//! These methods transfer ownership of the contained of an [`Option`]:
398421
//!
399-
//! * [`take`] takes ownership of the [`Option`], including any contained
400-
//! value, replacing it with [`None`]
401-
//! * [`replace`] takes ownership of the [`Option`], including any
402-
//! contained value, replacing it with a [`Some`] containing the provided
403-
//! value
422+
//! * [`take`] takes ownership of the contained value of an [`Option`], if
423+
//! any, replacing the [`Option`] with [`None`]
424+
//! * [`replace`] takes ownership of the contained value of an [`Option`],
425+
//! if any, replacing the [`Option`] with a [`Some`] containing the
426+
//! provided value
404427
//!
405-
//! [`take`]: Option::take
406428
//! [`replace`]: Option::replace
429+
//! [`take`]: Option::take
407430
//!
408431
//! # Examples
409432
//!
@@ -456,11 +479,6 @@
456479
//! None => println!("there are no animals :("),
457480
//! }
458481
//! ```
459-
//!
460-
//! [`Box<T>`]: ../../std/boxed/struct.Box.html
461-
//! [`Box<U>`]: ../../std/boxed/struct.Box.html
462-
//! [`num::NonZero*`]: crate::num
463-
//! [`ptr::NonNull<U>`]: crate::ptr::NonNull
464482
465483
#![stable(feature = "rust1", since = "1.0.0")]
466484

0 commit comments

Comments
 (0)