diff options
| author | Yuki Okushi <huyuumi.dev+love@gmail.com> | 2023-01-10 08:05:32 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-01-10 08:05:32 +0900 |
| commit | 002eccc7527c07a0aa89d58d5ef3c981f667b3f4 (patch) | |
| tree | 7fbff92825c9cf9eb7858fa3a53c3a8fdb6c0a25 | |
| parent | 3020239de947ec52677e9b4e853a6a9fc073d1f9 (diff) | |
| parent | c364d329ddda2ae2b7a553bf684a3e247977c003 (diff) | |
| download | rust-002eccc7527c07a0aa89d58d5ef3c981f667b3f4.tar.gz rust-002eccc7527c07a0aa89d58d5ef3c981f667b3f4.zip | |
Rollup merge of #105034 - HintringerFabian:improve_iterator_flatten_doc, r=cuviper
Add example for iterator_flatten Adds an Example to iterator_flatten Fixes #82687
| -rw-r--r-- | library/core/src/iter/traits/iterator.rs | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/library/core/src/iter/traits/iterator.rs b/library/core/src/iter/traits/iterator.rs index fc4d4bff24f..99aaf798e41 100644 --- a/library/core/src/iter/traits/iterator.rs +++ b/library/core/src/iter/traits/iterator.rs @@ -1514,6 +1514,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: /// /// ``` |
