|
49 | 49 | //! no "null" references. Instead, Rust has *optional* pointers, like
|
50 | 50 | //! the optional owned box, [`Option`]`<`[`Box<T>`]`>`.
|
51 | 51 | //!
|
| 52 | +//! [`Box<T>`]: ../../std/boxed/struct.Box.html |
| 53 | +//! |
52 | 54 | //! The following example uses [`Option`] to create an optional box of
|
53 | 55 | //! [`i32`]. Notice that in order to use the inner [`i32`] value, the
|
54 | 56 | //! `check_optional` function first needs to use pattern matching to
|
|
83 | 85 | //! * [`ptr::NonNull<U>`]
|
84 | 86 | //! * `#[repr(transparent)]` struct around one of the types in this list.
|
85 | 87 | //!
|
| 88 | +//! [`Box<U>`]: ../../std/boxed/struct.Box.html |
| 89 | +//! [`num::NonZero*`]: crate::num |
| 90 | +//! [`ptr::NonNull<U>`]: crate::ptr::NonNull |
| 91 | +//! |
86 | 92 | //! This is called the "null pointer optimization" or NPO.
|
87 | 93 | //!
|
88 | 94 | //! It is further guaranteed that, for the cases above, one can
|
|
100 | 106 | //! The [`is_some`] and [`is_none`] methods return [`true`] if the [`Option`]
|
101 | 107 | //! is [`Some`] or [`None`], respectively.
|
102 | 108 | //!
|
103 |
| -//! [`is_some`]: Option::is_some |
104 | 109 | //! [`is_none`]: Option::is_none
|
| 110 | +//! [`is_some`]: Option::is_some |
105 | 111 | //!
|
106 | 112 | //! ## Adapters for working with references
|
107 | 113 | //!
|
108 | 114 | //! * [`as_ref`] converts from `&Option<T>` to `Option<&T>`
|
109 | 115 | //! * [`as_mut`] converts from `&mut Option<T>` to `Option<&mut T>`
|
110 | 116 | //! * [`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>>` |
114 | 123 | //!
|
115 |
| -//! [`&mut Pin`]: crate::pin::Pin |
116 |
| -//! [`&Pin`]: crate::pin::Pin |
117 | 124 | //! [`as_deref`]: Option::as_deref
|
118 | 125 | //! [`as_deref_mut`]: Option::as_deref_mut
|
119 | 126 | //! [`as_mut`]: Option::as_mut
|
120 |
| -//! [`as_pin_ref`]: Option::as_pin_ref |
121 | 127 | //! [`as_pin_mut`]: Option::as_pin_mut
|
| 128 | +//! [`as_pin_ref`]: Option::as_pin_ref |
122 | 129 | //! [`as_ref`]: Option::as_ref
|
123 |
| -//! [`Pin`]: crate::pin::Pin |
124 | 130 | //!
|
125 | 131 | //! ## Extracting the contained value
|
126 | 132 | //!
|
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`]: |
129 | 135 | //!
|
130 | 136 | //! * [`expect`] panics with a provided custom message
|
131 | 137 | //! * [`unwrap`] panics with a generic message
|
|
135 | 141 | //! * [`unwrap_or_else`] returns the result of evaluating the provided
|
136 | 142 | //! function
|
137 | 143 | //!
|
138 |
| -//! [`Default`]: crate::default::Default |
139 | 144 | //! [`expect`]: Option::expect
|
140 | 145 | //! [`unwrap`]: Option::unwrap
|
141 | 146 | //! [`unwrap_or`]: Option::unwrap_or
|
|
144 | 149 | //!
|
145 | 150 | //! ## Transforming contained values
|
146 | 151 | //!
|
147 |
| -//! These transformations are from [`Option`] to [`Result`]. |
| 152 | +//! These methods transform [`Option`] to [`Result`]: |
148 | 153 | //!
|
149 | 154 | //! * [`ok_or`] transforms [`Some(v)`] to [`Ok(v)`], and [`None`] to
|
150 | 155 | //! [`Err(err)`] using the provided default `err` value
|
|
153 | 158 | //! * [`transpose`] transposes an [`Option`] of a [`Result`] into a
|
154 | 159 | //! [`Result`] of an [`Option`]
|
155 | 160 | //!
|
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: |
157 | 169 | //!
|
158 | 170 | //! * [`filter`] calls the provided predicate function on the contained
|
159 | 171 | //! value `t` if the [`Option`] is [`Some(t)`], and returns [`Some(t)`]
|
|
163 | 175 | //! * [`map`] transforms [`Option<T>`] to [`Option<U>`] by applying the
|
164 | 176 | //! provided function to the contained value of [`Some`] and leaving
|
165 | 177 | //! [`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 |
173 | 178 | //!
|
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: |
175 | 198 | //!
|
176 | 199 | //! * [`zip`] returns [`Some((s, o))`] if `self` is [`Some(s)`] and the
|
177 | 200 | //! provided [`Option`] value is [`Some(o)`]; otherwise, returns [`None`]
|
178 | 201 | //! * [`zip_with`] calls the provided function `f` and returns
|
179 | 202 | //! [`Some(f(s, o))`] if `self` is [`Some(s)`] and the provided
|
180 | 203 | //! [`Option`] value is [`Some(o)`]; otherwise, returns [`None`]
|
181 | 204 | //!
|
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 |
191 | 205 | //! [`Some(f(s, o))`]: Some
|
192 | 206 | //! [`Some(o)`]: Some
|
193 | 207 | //! [`Some(s)`]: Some
|
194 | 208 | //! [`Some((s, o))`]: Some
|
195 |
| -//! [`Some(t)`]: Some |
196 |
| -//! [`Some(v)`]: Some |
197 |
| -//! [`transpose`]: Option::transpose |
198 | 209 | //! [`zip`]: Option::zip
|
199 | 210 | //! [`zip_with`]: Option::zip_with
|
200 | 211 | //!
|
|
223 | 234 | //! | [`xor`] | `Some(x)` | `None` | `Some(x)` |
|
224 | 235 | //! | [`xor`] | `Some(x)` | `Some(y)` | `None` |
|
225 | 236 | //!
|
| 237 | +//! [`and`]: Option::and |
| 238 | +//! [`or`]: Option::or |
| 239 | +//! [`xor`]: Option::xor |
| 240 | +//! |
226 | 241 | //! The [`and_then`] and [`or_else`] methods take a function as input, and
|
227 | 242 | //! only evaluate the function when they need to produce a new value. Only
|
228 | 243 | //! the [`and_then`] method can produce an [`Option<U>`] value having a
|
|
237 | 252 | //! | [`or_else`] | `None` | (not provided) | `Some(y)` | `Some(y)` |
|
238 | 253 | //! | [`or_else`] | `Some(x)` | (not provided) | (not evaluated) | `Some(x)` |
|
239 | 254 | //!
|
240 |
| -//! [`and`]: Option::and |
241 | 255 | //! [`and_then`]: Option::and_then
|
242 |
| -//! [`or`]: Option::or |
243 | 256 | //! [`or_else`]: Option::or_else
|
244 |
| -//! [`xor`]: Option::xor |
245 | 257 | //!
|
246 | 258 | //! This is an example of using methods like [`and_then`] and [`or`] in a
|
247 | 259 | //! pipeline of method calls. Early stages of the pipeline pass failure
|
|
282 | 294 | //! [`once(v)`] if the [`Option`] is [`Some(v)`], and like [`empty()`] if
|
283 | 295 | //! the [`Option`] is [`None`].
|
284 | 296 | //!
|
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: |
286 | 302 | //!
|
287 | 303 | //! * [`into_iter`] consumes the [`Option`] and produces the contained
|
288 | 304 | //! value
|
|
291 | 307 | //! * [`iter_mut`] produces a mutable reference of type `&mut T` to the
|
292 | 308 | //! contained value
|
293 | 309 | //!
|
294 |
| -//! [`empty()`]: crate::iter::empty |
295 | 310 | //! [`into_iter`]: Option::into_iter
|
296 | 311 | //! [`iter`]: Option::iter
|
297 | 312 | //! [`iter_mut`]: Option::iter_mut
|
298 |
| -//! [`once(v)`]: crate::iter::once |
299 |
| -//! [`Some(v)`]: Some |
300 | 313 | //!
|
301 | 314 | //! An iterator over [`Option`] can be useful when chaining iterators, for
|
302 | 315 | //! example, to conditionally insert items. (It's not always necessary to
|
|
334 | 347 | //! we can't return `impl Iterator` anymore because the concrete types of
|
335 | 348 | //! the return values differ.
|
336 | 349 | //!
|
| 350 | +//! [`empty()`]: crate::iter::empty |
| 351 | +//! [`once()`]: crate::iter::once |
| 352 | +//! |
337 | 353 | //! ```compile_fail,E0308
|
338 | 354 | //! # use std::iter::{empty, once};
|
339 | 355 | //! // This won't compile because all possible returns from the function
|
|
347 | 363 | //! }
|
348 | 364 | //! ```
|
349 | 365 | //!
|
350 |
| -//! [`once()`]: crate::iter::once |
351 |
| -//! |
352 | 366 | //! ## Collecting into `Option`
|
353 | 367 | //!
|
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`]. |
358 | 372 | //!
|
359 |
| -//! [`FromIterator`]: Option#impl-FromIterator%3COption%3CA%3E%3E |
| 373 | +//! [impl-FromIterator]: Option#impl-FromIterator%3COption%3CA%3E%3E |
360 | 374 | //!
|
361 | 375 | //! ```
|
362 | 376 | //! let v = vec![Some(2), Some(4), None, Some(8)];
|
|
367 | 381 | //! assert_eq!(res, Some(vec![2, 4, 8]));
|
368 | 382 | //! ```
|
369 | 383 | //!
|
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. |
373 | 388 | //!
|
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 | +//! ``` |
378 | 400 | //!
|
379 | 401 | //! ## Modifying an [`Option`] in-place
|
380 | 402 | //!
|
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>`]: |
383 | 405 | //!
|
384 | 406 | //! * [`insert`] inserts a value, dropping any old contents
|
385 | 407 | //! * [`get_or_insert`] gets the current value, inserting a provided
|
386 | 408 | //! default value if it is [`None`]
|
387 | 409 | //! * [`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`] |
389 | 412 | //! * [`get_or_insert_with`] gets the current value, inserting a default
|
390 | 413 | //! computed by the provided function if it is [`None`]
|
391 | 414 | //!
|
392 |
| -//! [`insert`]: Option::insert |
393 | 415 | //! [`get_or_insert`]: Option::get_or_insert
|
394 | 416 | //! [`get_or_insert_default`]: Option::get_or_insert_default
|
395 | 417 | //! [`get_or_insert_with`]: Option::get_or_insert_with
|
| 418 | +//! [`insert`]: Option::insert |
396 | 419 | //!
|
397 |
| -//! These methods transfer ownership of the [`Option`]. |
| 420 | +//! These methods transfer ownership of the contained of an [`Option`]: |
398 | 421 | //!
|
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 |
404 | 427 | //!
|
405 |
| -//! [`take`]: Option::take |
406 | 428 | //! [`replace`]: Option::replace
|
| 429 | +//! [`take`]: Option::take |
407 | 430 | //!
|
408 | 431 | //! # Examples
|
409 | 432 | //!
|
|
456 | 479 | //! None => println!("there are no animals :("),
|
457 | 480 | //! }
|
458 | 481 | //! ```
|
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 |
464 | 482 |
|
465 | 483 | #![stable(feature = "rust1", since = "1.0.0")]
|
466 | 484 |
|
|
0 commit comments