From 76438d26b1809c1499859f2943709e40f8df450c Mon Sep 17 00:00:00 2001 From: Fabian Hintringer Date: Mon, 28 Nov 2022 23:01:15 +0100 Subject: [PATCH 1/3] Add example for iterator_flatten --- library/core/src/iter/traits/iterator.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/library/core/src/iter/traits/iterator.rs b/library/core/src/iter/traits/iterator.rs index 83c7e8977e9..6e9b48cb794 100644 --- a/library/core/src/iter/traits/iterator.rs +++ b/library/core/src/iter/traits/iterator.rs @@ -1495,6 +1495,14 @@ pub trait Iterator { /// assert_eq!(merged, "alphabetagamma"); /// ``` /// + /// Flattening also works on other types like Option and Result: + /// + /// ``` + /// let values = vec![Some(123), Some(321), None, Some(231)]; + /// let flattened_values: Vec<_> = values.into_iter().flatten().collect(); + /// assert_eq!(flattened_values, vec![123, 321, 231]); + /// ``` + /// /// You can also rewrite this in terms of [`flat_map()`], which is preferable /// in this case since it conveys intent more clearly: /// From 083560b7d84fd3660a6ba99acb7f2570296063bd Mon Sep 17 00:00:00 2001 From: Fabian Hintringer Date: Tue, 13 Dec 2022 09:17:22 +0100 Subject: [PATCH 2/3] Add result example + rewording --- library/core/src/iter/traits/iterator.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/library/core/src/iter/traits/iterator.rs b/library/core/src/iter/traits/iterator.rs index 6e9b48cb794..53d353802b9 100644 --- a/library/core/src/iter/traits/iterator.rs +++ b/library/core/src/iter/traits/iterator.rs @@ -1495,12 +1495,16 @@ pub trait Iterator { /// assert_eq!(merged, "alphabetagamma"); /// ``` /// - /// Flattening also works on other types like Option and Result: + /// Flattening works on any `IntoIterator` type, including `Option` and `Result`: /// /// ``` - /// let values = vec![Some(123), Some(321), None, Some(231)]; - /// let flattened_values: Vec<_> = values.into_iter().flatten().collect(); - /// assert_eq!(flattened_values, vec![123, 321, 231]); + /// let options = vec![Some(123), Some(321), None, Some(231)]; + /// let flattened_options: Vec<_> = options.into_iter().flatten().collect(); + /// assert_eq!(flattened_options, vec![123, 321, 231]); + /// + /// let results = vec![Ok(123), Ok(321), Err(456), Ok(231)]; + /// let flattened_results: Vec<_> = results.into_iter().flatten().collect(); + /// assert_eq!(flattened_results, vec![123, 321, 231]); /// ``` /// /// You can also rewrite this in terms of [`flat_map()`], which is preferable From c364d329ddda2ae2b7a553bf684a3e247977c003 Mon Sep 17 00:00:00 2001 From: Fabian Hintringer Date: Mon, 9 Jan 2023 13:19:41 +0100 Subject: [PATCH 3/3] Relocate changes --- library/core/src/iter/traits/iterator.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/library/core/src/iter/traits/iterator.rs b/library/core/src/iter/traits/iterator.rs index 53d353802b9..353cb147f10 100644 --- a/library/core/src/iter/traits/iterator.rs +++ b/library/core/src/iter/traits/iterator.rs @@ -1495,18 +1495,6 @@ pub trait Iterator { /// assert_eq!(merged, "alphabetagamma"); /// ``` /// - /// Flattening works on any `IntoIterator` type, including `Option` and `Result`: - /// - /// ``` - /// let options = vec![Some(123), Some(321), None, Some(231)]; - /// let flattened_options: Vec<_> = options.into_iter().flatten().collect(); - /// assert_eq!(flattened_options, vec![123, 321, 231]); - /// - /// let results = vec![Ok(123), Ok(321), Err(456), Ok(231)]; - /// let flattened_results: Vec<_> = results.into_iter().flatten().collect(); - /// assert_eq!(flattened_results, vec![123, 321, 231]); - /// ``` - /// /// You can also rewrite this in terms of [`flat_map()`], which is preferable /// in this case since it conveys intent more clearly: /// @@ -1520,6 +1508,18 @@ pub trait Iterator { /// assert_eq!(merged, "alphabetagamma"); /// ``` /// + /// Flattening works on any `IntoIterator` type, including `Option` and `Result`: + /// + /// ``` + /// let options = vec![Some(123), Some(321), None, Some(231)]; + /// let flattened_options: Vec<_> = options.into_iter().flatten().collect(); + /// assert_eq!(flattened_options, vec![123, 321, 231]); + /// + /// let results = vec![Ok(123), Ok(321), Err(456), Ok(231)]; + /// let flattened_results: Vec<_> = results.into_iter().flatten().collect(); + /// assert_eq!(flattened_results, vec![123, 321, 231]); + /// ``` + /// /// Flattening only removes one level of nesting at a time: /// /// ```