Skip to content

Commit 5d74e88

Browse files
committed
Auto merge of rust-lang#75949 - vext01:filter-docs, r=jyn514
Try to improve the documentation of `filter()` and `filter_map()`. I believe the documentation is currently a little misleading. For example, in the docs for `filter()`: > If the closure returns `false`, it will try again, and call the closure on > the next element, seeing if it passes the test. This kind of implies that if the closure returns true then we *don't* "try again" and no further elements are considered. In actuality that's not the case, every element is tried regardless of what happened with the previous element. This change tries to clarify that by removing the uses of "try again" altogether.
2 parents ac892a1 + 8af85fa commit 5d74e88

File tree

1 file changed

+8
-18
lines changed

1 file changed

+8
-18
lines changed

library/core/src/iter/traits/iterator.rs

+8-18
Original file line numberDiff line numberDiff line change
@@ -648,11 +648,9 @@ pub trait Iterator {
648648
/// Creates an iterator which uses a closure to determine if an element
649649
/// should be yielded.
650650
///
651-
/// The closure must return `true` or `false`. `filter()` creates an
652-
/// iterator which calls this closure on each element. If the closure
653-
/// returns `true`, then the element is returned. If the closure returns
654-
/// `false`, it will try again, and call the closure on the next element,
655-
/// seeing if it passes the test.
651+
/// Given an element the closure must return `true` or `false`. The returned
652+
/// iterator will yield only the elements for which the closure returns
653+
/// true.
656654
///
657655
/// # Examples
658656
///
@@ -719,24 +717,16 @@ pub trait Iterator {
719717

720718
/// Creates an iterator that both filters and maps.
721719
///
722-
/// The closure must return an [`Option<T>`]. `filter_map` creates an
723-
/// iterator which calls this closure on each element. If the closure
724-
/// returns [`Some(element)`][`Some`], then that element is returned. If the
725-
/// closure returns [`None`], it will try again, and call the closure on the
726-
/// next element, seeing if it will return [`Some`].
720+
/// The returned iterator yields only the `value`s for which the supplied
721+
/// closure returns `Some(value)`.
727722
///
728-
/// Why `filter_map` and not just [`filter`] and [`map`]? The key is in this
729-
/// part:
723+
/// `filter_map` can be used to make chains of [`filter`] and [`map`] more
724+
/// concise. The example below shows how a `map().filter().map()` can be
725+
/// shortened to a single call to `filter_map`.
730726
///
731727
/// [`filter`]: Iterator::filter
732728
/// [`map`]: Iterator::map
733729
///
734-
/// > If the closure returns [`Some(element)`][`Some`], then that element is returned.
735-
///
736-
/// In other words, it removes the [`Option<T>`] layer automatically. If your
737-
/// mapping is already returning an [`Option<T>`] and you want to skip over
738-
/// [`None`]s, then `filter_map` is much, much nicer to use.
739-
///
740730
/// # Examples
741731
///
742732
/// Basic usage:

0 commit comments

Comments
 (0)