|
132 | 132 | //! * [`unwrap_or`] returns the provided default value
|
133 | 133 | //! * [`unwrap_or_default`] returns the default value of the type `T`
|
134 | 134 | //! (which must implement the [`Default`] trait)
|
135 |
| -//! * [`unwrap_or_else`] evaluates a provided function |
| 135 | +//! * [`unwrap_or_else`] returns the result of evaluating the provided |
| 136 | +//! function |
136 | 137 | //!
|
137 | 138 | //! [`Default`]: crate::default::Default
|
138 | 139 | //! [`expect`]: Option::expect
|
|
143 | 144 | //!
|
144 | 145 | //! ## Transforming contained values
|
145 | 146 | //!
|
| 147 | +//! * [`filter`] calls the provided predicate function on the contained |
| 148 | +//! value `t` if the [`Option`] is [`Some(t)`], and returns [`Some(t)`] |
| 149 | +//! if the function returns `true`; otherwise, returns [`None`] |
146 | 150 | //! * [`flatten`] removes one level of nesting from an
|
147 | 151 | //! [`Option<Option<T>>`]
|
148 | 152 | //! * [`map`] transforms [`Some<T>`] to [`Some<U>`] using the provided
|
|
159 | 163 | //! a value of [`Err<E>`] using the provided function
|
160 | 164 | //! * [`transpose`] transposes an [`Option`] of a [`Result`] into a
|
161 | 165 | //! [`Result`] of an [`Option`]
|
| 166 | +//! * [`zip`] returns [`Some((s, o))`] if `self` is [`Some(s)`] and the |
| 167 | +//! provided [`Option`] value is [`Some(o)`]; otherwise, returns [`None`] |
| 168 | +//! * [`zip_with`] calls the provided function `f` and returns |
| 169 | +//! [`Some(f(s, o))`] if `self` is [`Some(s)`] and the provided |
| 170 | +//! [`Option`] value is [`Some(o)`]; otherwise, returns [`None`] |
162 | 171 | //!
|
163 | 172 | //! [`Err(err)`]: Err
|
| 173 | +//! [`filter`]: Option::filter |
164 | 174 | //! [`flatten`]: Option::flatten
|
165 | 175 | //! [`map`]: Option::map
|
166 | 176 | //! [`map_or`]: Option::map_or
|
167 | 177 | //! [`map_or_else`]: Option::map_or_else
|
168 | 178 | //! [`Ok(v)`]: Ok
|
169 | 179 | //! [`ok_or`]: Option::ok_or
|
170 | 180 | //! [`ok_or_else`]: Option::ok_or_else
|
| 181 | +//! [`Some(f(s, o))`]: Some |
| 182 | +//! [`Some(o)`]: Some |
| 183 | +//! [`Some(s)`]: Some |
| 184 | +//! [`Some((s, o))`]: Some |
| 185 | +//! [`Some(t)`]: Some |
171 | 186 | //! [`Some(v)`]: Some
|
172 | 187 | //! [`transpose`]: Option::transpose
|
| 188 | +//! [`zip`]: Option::zip |
| 189 | +//! [`zip_with`]: Option::zip_with |
173 | 190 | //!
|
174 | 191 | //! ## Boolean operators
|
175 | 192 | //!
|
|
196 | 213 | //! | [`xor`] | `Some(x)` | `None` | `Some(x)` |
|
197 | 214 | //! | [`xor`] | `Some(x)` | `Some(y)` | `None` |
|
198 | 215 | //!
|
199 |
| -//! The [`and_then`], [`filter`], and [`or_else`] methods take a function |
200 |
| -//! as input, and only evaluate the function when they need to produce a |
201 |
| -//! new value. [`and_then`] and [`or_else`] take a function that produces |
202 |
| -//! another [`Option`] value, while [`filter`] takes a predicate that is |
203 |
| -//! used to decide whether to pass the [`Some`] value through. Only the |
204 |
| -//! [`and_then`] method can produce an [`Option<U>`] value having a |
| 216 | +//! The [`and_then`] and [`or_else`] methods take a function as input, and |
| 217 | +//! only evaluate the function when they need to produce a new value. Only |
| 218 | +//! the [`and_then`] method can produce an [`Option<U>`] value having a |
205 | 219 | //! different inner type `U` than [`Option<T>`].
|
206 | 220 | //!
|
207 | 221 | //! | method | self | function input | function result | output |
|
208 | 222 | //! |--------------|-----------|----------------|-----------------|-----------|
|
209 | 223 | //! | [`and_then`] | `None` | (not provided) | (not evaluated) | `None` |
|
210 | 224 | //! | [`and_then`] | `Some(x)` | `x` | `None` | `None` |
|
211 | 225 | //! | [`and_then`] | `Some(x)` | `x` | `Some(y)` | `Some(y)` |
|
212 |
| -//! | [`filter`] | `None` | (not provided) | (not evaluated) | `None` | |
213 |
| -//! | [`filter`] | `Some(x)` | `x` | `false` | `None` | |
214 |
| -//! | [`filter`] | `Some(x)` | `x` | `true` | `Some(x)` | |
215 | 226 | //! | [`or_else`] | `None` | (not provided) | `None` | `None` |
|
216 | 227 | //! | [`or_else`] | `None` | (not provided) | `Some(y)` | `Some(y)` |
|
217 | 228 | //! | [`or_else`] | `Some(x)` | (not provided) | (not evaluated) | `Some(x)` |
|
218 | 229 | //!
|
219 | 230 | //! [`and`]: Option::and
|
220 | 231 | //! [`and_then`]: Option::and_then
|
221 |
| -//! [`filter`]: Option::filter |
222 | 232 | //! [`or`]: Option::or
|
223 | 233 | //! [`or_else`]: Option::or_else
|
224 | 234 | //! [`xor`]: Option::xor
|
225 | 235 | //!
|
226 |
| -//! ## Iterators |
| 236 | +//! ## Iterating over `Option` |
227 | 237 | //!
|
228 | 238 | //! An [`Option`] can be iterated over. This can be helpful if you need an
|
229 | 239 | //! iterator that is conditionally empty. The iterator will either produce
|
|
241 | 251 | //! * [`iter_mut`] produces a mutable reference of type `&mut T` to the
|
242 | 252 | //! contained value
|
243 | 253 | //!
|
244 |
| -//! [`Option`] implements the [`FromIterator`] trait, which allows an |
245 |
| -//! iterator over [`Option`] values to be collected into an [`Option`] of a |
246 |
| -//! collection of each contained value of the original [`Option`] values, |
247 |
| -//! or [`None`] if any of the elements was [`None`]. |
248 |
| -//! |
249 | 254 | //! [`empty()`]: crate::iter::empty
|
250 |
| -//! [`FromIterator`]: Option#impl-FromIterator%3COption%3CA%3E%3E |
251 | 255 | //! [`into_iter`]: Option::into_iter
|
252 | 256 | //! [`iter`]: Option::iter
|
253 | 257 | //! [`iter_mut`]: Option::iter_mut
|
254 | 258 | //! [`once(v)`]: crate::iter::once
|
255 | 259 | //! [`Some(v)`]: Some
|
256 | 260 | //!
|
257 |
| -//! An iterator over [`Option`] can be useful when chaining iterators: |
| 261 | +//! An iterator over [`Option`] can be useful when chaining iterators, for |
| 262 | +//! example, to conditionally insert items. (It's not always necessary to |
| 263 | +//! explicitly call an iterator constructor: many [`Iterator`] methods that |
| 264 | +//! accept other iterators will also accept iterable types that implement |
| 265 | +//! [`IntoIterator`], which includes [`Option`].) |
258 | 266 | //!
|
259 | 267 | //! ```
|
260 | 268 | //! let yep = Some(42);
|
261 | 269 | //! let nope = None;
|
262 |
| -//! let nums: Vec<i32> = (0..4).chain(yep.into_iter()).chain(4..8).collect(); |
| 270 | +//! // chain() already calls into_iter(), so we don't have to do so |
| 271 | +//! let nums: Vec<i32> = (0..4).chain(yep).chain(4..8).collect(); |
263 | 272 | //! assert_eq!(nums, [0, 1, 2, 3, 42, 4, 5, 6, 7]);
|
264 |
| -//! let nums: Vec<i32> = (0..4).chain(nope.into_iter()).chain(4..8).collect(); |
| 273 | +//! let nums: Vec<i32> = (0..4).chain(nope).chain(4..8).collect(); |
265 | 274 | //! assert_eq!(nums, [0, 1, 2, 3, 4, 5, 6, 7]);
|
266 | 275 | //! ```
|
267 | 276 | //!
|
|
270 | 279 | //! concrete type. Chaining an iterated [`Option`] can help with that.
|
271 | 280 | //!
|
272 | 281 | //! ```
|
273 |
| -//! let yep = Some(42); |
274 |
| -//! let nope = None; |
275 |
| -//! |
276 |
| -//! fn make_iter(opt: Option<i32>) -> impl Iterator<Item = i32> { |
277 |
| -//! (0..4).chain(opt.into_iter()).chain(4..8) |
| 282 | +//! fn make_iter(do_insert: bool) -> impl Iterator<Item = i32> { |
| 283 | +//! // Explicit returns to illustrate return types matching |
| 284 | +//! match do_insert { |
| 285 | +//! true => return (0..4).chain(Some(42)).chain(4..8), |
| 286 | +//! false => return (0..4).chain(None).chain(4..8), |
| 287 | +//! } |
278 | 288 | //! }
|
279 |
| -//! println!("{:?}", make_iter(yep).collect::<Vec<_>>()); |
280 |
| -//! println!("{:?}", make_iter(nope).collect::<Vec<_>>()); |
| 289 | +//! println!("{:?}", make_iter(true).collect::<Vec<_>>()); |
| 290 | +//! println!("{:?}", make_iter(false).collect::<Vec<_>>()); |
281 | 291 | //! ```
|
282 | 292 | //!
|
283 |
| -//! If we try to do the same thing, but using pattern matching, we can't |
284 |
| -//! return `impl Iterator` anymore because the concrete types of the return |
285 |
| -//! values differ. |
| 293 | +//! If we try to do the same thing, but using [`once()`] and [`empty()`], |
| 294 | +//! we can't return `impl Iterator` anymore because the concrete types of |
| 295 | +//! the return values differ. |
286 | 296 | //!
|
287 | 297 | //! ```compile_fail,E0308
|
288 | 298 | //! # use std::iter::{empty, once};
|
289 | 299 | //! // This won't compile because all possible returns from the function
|
290 | 300 | //! // must have the same concrete type.
|
291 |
| -//! fn make_iter(opt: Option<i32>) -> impl Iterator<Item = i32> { |
292 |
| -//! match opt { |
293 |
| -//! Some(x) => return (0..4).chain(once(x)).chain(4..8), |
294 |
| -//! None => return (0..4).chain(empty()).chain(4..8) |
| 301 | +//! fn make_iter(do_insert: bool) -> impl Iterator<Item = i32> { |
| 302 | +//! // Explicit returns to illustrate return types not matching |
| 303 | +//! match x { |
| 304 | +//! true => return (0..4).chain(once(42)).chain(4..8), |
| 305 | +//! false => return (0..4).chain(empty()).chain(4..8), |
295 | 306 | //! }
|
296 | 307 | //! }
|
297 | 308 | //! ```
|
298 | 309 | //!
|
| 310 | +//! [`once()`]: crate::iter::once |
| 311 | +//! |
| 312 | +//! ## Collecting into `Option` |
| 313 | +//! |
| 314 | +//! [`Option`] implements the [`FromIterator`] trait, which allows an |
| 315 | +//! iterator over [`Option`] values to be collected into an [`Option`] of a |
| 316 | +//! collection of each contained value of the original [`Option`] values, |
| 317 | +//! or [`None`] if any of the elements was [`None`]. |
| 318 | +//! |
| 319 | +//! [`FromIterator`]: Option#impl-FromIterator%3COption%3CA%3E%3E |
| 320 | +//! |
| 321 | +//! ``` |
| 322 | +//! let v = vec![Some(2), Some(4), None, Some(8)]; |
| 323 | +//! let res: Option<Vec<_>> = v.into_iter().collect(); |
| 324 | +//! assert_eq!(res, None); |
| 325 | +//! let v = vec![Some(2), Some(4), Some(8)]; |
| 326 | +//! let res: Option<Vec<_>> = v.into_iter().collect(); |
| 327 | +//! assert_eq!(res, Some(vec![2, 4, 8])); |
| 328 | +//! ``` |
| 329 | +//! |
| 330 | +//! [`Option`] also implements the [`Product`] and [`Sum`] traits, allowing |
| 331 | +//! an iterator over [`Option`] values to provide the |
| 332 | +//! [`product`][m.product] and [`sum`][m.sum] methods. |
| 333 | +//! |
| 334 | +//! [`Product`]: Option#impl-Product%3COption%3CU%3E%3E |
| 335 | +//! [`Sum`]: Option#impl-Sum%3COption%3CU%3E%3E |
| 336 | +//! [m.product]: crate::iter::Iterator::product |
| 337 | +//! [m.sum]: crate::iter::Iterator::sum |
| 338 | +//! |
299 | 339 | //! ## Modifying an [`Option`] in-place
|
300 | 340 | //!
|
301 | 341 | //! These methods return a mutable reference to the contained value of a
|
|
0 commit comments