Skip to content

Commit 22201c3

Browse files
committed
updates based on feedback
Make minor wording changes in a few places. Move `filter` to the "transformations" section. Add `zip` methods to the "transformations" section. Clarify the section about `Option` iterators, and add a section about collecting into `Option`. Clarify that for `Result`, `or` and `or_else` can also produce a `Result` having a different type.
1 parent 1648415 commit 22201c3

File tree

2 files changed

+83
-40
lines changed

2 files changed

+83
-40
lines changed

core/src/option.rs

+75-35
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@
132132
//! * [`unwrap_or`] returns the provided default value
133133
//! * [`unwrap_or_default`] returns the default value of the type `T`
134134
//! (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
136137
//!
137138
//! [`Default`]: crate::default::Default
138139
//! [`expect`]: Option::expect
@@ -143,6 +144,9 @@
143144
//!
144145
//! ## Transforming contained values
145146
//!
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`]
146150
//! * [`flatten`] removes one level of nesting from an
147151
//! [`Option<Option<T>>`]
148152
//! * [`map`] transforms [`Some<T>`] to [`Some<U>`] using the provided
@@ -159,17 +163,30 @@
159163
//! a value of [`Err<E>`] using the provided function
160164
//! * [`transpose`] transposes an [`Option`] of a [`Result`] into a
161165
//! [`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`]
162171
//!
163172
//! [`Err(err)`]: Err
173+
//! [`filter`]: Option::filter
164174
//! [`flatten`]: Option::flatten
165175
//! [`map`]: Option::map
166176
//! [`map_or`]: Option::map_or
167177
//! [`map_or_else`]: Option::map_or_else
168178
//! [`Ok(v)`]: Ok
169179
//! [`ok_or`]: Option::ok_or
170180
//! [`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
171186
//! [`Some(v)`]: Some
172187
//! [`transpose`]: Option::transpose
188+
//! [`zip`]: Option::zip
189+
//! [`zip_with`]: Option::zip_with
173190
//!
174191
//! ## Boolean operators
175192
//!
@@ -196,34 +213,27 @@
196213
//! | [`xor`] | `Some(x)` | `None` | `Some(x)` |
197214
//! | [`xor`] | `Some(x)` | `Some(y)` | `None` |
198215
//!
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
205219
//! different inner type `U` than [`Option<T>`].
206220
//!
207221
//! | method | self | function input | function result | output |
208222
//! |--------------|-----------|----------------|-----------------|-----------|
209223
//! | [`and_then`] | `None` | (not provided) | (not evaluated) | `None` |
210224
//! | [`and_then`] | `Some(x)` | `x` | `None` | `None` |
211225
//! | [`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)` |
215226
//! | [`or_else`] | `None` | (not provided) | `None` | `None` |
216227
//! | [`or_else`] | `None` | (not provided) | `Some(y)` | `Some(y)` |
217228
//! | [`or_else`] | `Some(x)` | (not provided) | (not evaluated) | `Some(x)` |
218229
//!
219230
//! [`and`]: Option::and
220231
//! [`and_then`]: Option::and_then
221-
//! [`filter`]: Option::filter
222232
//! [`or`]: Option::or
223233
//! [`or_else`]: Option::or_else
224234
//! [`xor`]: Option::xor
225235
//!
226-
//! ## Iterators
236+
//! ## Iterating over `Option`
227237
//!
228238
//! An [`Option`] can be iterated over. This can be helpful if you need an
229239
//! iterator that is conditionally empty. The iterator will either produce
@@ -241,27 +251,26 @@
241251
//! * [`iter_mut`] produces a mutable reference of type `&mut T` to the
242252
//! contained value
243253
//!
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-
//!
249254
//! [`empty()`]: crate::iter::empty
250-
//! [`FromIterator`]: Option#impl-FromIterator%3COption%3CA%3E%3E
251255
//! [`into_iter`]: Option::into_iter
252256
//! [`iter`]: Option::iter
253257
//! [`iter_mut`]: Option::iter_mut
254258
//! [`once(v)`]: crate::iter::once
255259
//! [`Some(v)`]: Some
256260
//!
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`].)
258266
//!
259267
//! ```
260268
//! let yep = Some(42);
261269
//! 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();
263272
//! 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();
265274
//! assert_eq!(nums, [0, 1, 2, 3, 4, 5, 6, 7]);
266275
//! ```
267276
//!
@@ -270,32 +279,63 @@
270279
//! concrete type. Chaining an iterated [`Option`] can help with that.
271280
//!
272281
//! ```
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+
//! }
278288
//! }
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<_>>());
281291
//! ```
282292
//!
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.
286296
//!
287297
//! ```compile_fail,E0308
288298
//! # use std::iter::{empty, once};
289299
//! // This won't compile because all possible returns from the function
290300
//! // 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),
295306
//! }
296307
//! }
297308
//! ```
298309
//!
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+
//!
299339
//! ## Modifying an [`Option`] in-place
300340
//!
301341
//! These methods return a mutable reference to the contained value of a

core/src/result.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,10 @@
235235
//! ones that take a function as input (to be lazily evaluated).
236236
//!
237237
//! The [`and`] and [`or`] methods take another [`Result`] as input, and
238-
//! produce an [`Result`] as output. Only the [`and`] method can produce a
238+
//! produce a [`Result`] as output. The [`and`] method can produce a
239239
//! [`Result<U, E>`] value having a different inner type `U` than
240-
//! [`Result<T, E>`].
240+
//! [`Result<T, E>`]. The [`or`] method can produce a [`Result<T, F>`]
241+
//! value having a different error type `F` than [`Result<T, E>`].
241242
//!
242243
//! | method | self | input | output |
243244
//! |---------|----------|-----------|----------|
@@ -249,9 +250,11 @@
249250
//! | [`or`] | `Ok(x)` | (ignored) | `Ok(x)` |
250251
//!
251252
//! The [`and_then`] and [`or_else`] methods take a function as input, and
252-
//! only evaluate the function when they need to produce a new value. Only
253-
//! the [`and_then`] method can produce an [`Result<U, E>`] value having a
254-
//! different inner type `U` than [`Result<T, E>`].
253+
//! only evaluate the function when they need to produce a new value. The
254+
//! [`and_then`] method can produce a [`Result<U, E>`] value having a
255+
//! different inner type `U` than [`Result<T, E>`]. The [`or_else`] method
256+
//! can produce a [`Result<T, F>`] value having a different error type `F`
257+
//! than [`Result<T, E>`].
255258
//!
256259
//! | method | self | function input | function result | output |
257260
//! |--------------|----------|----------------|-----------------|----------|

0 commit comments

Comments
 (0)