]> git.lizzy.rs Git - rust.git/blobdiff - library/core/src/iter/traits/iterator.rs
Rollup merge of #105034 - HintringerFabian:improve_iterator_flatten_doc, r=cuviper
[rust.git] / library / core / src / iter / traits / iterator.rs
index fc4d4bff24f33f4a9d58a2953dd9ef562b75cf2b..99aaf798e411fb01dbea3c1c5b0286e014fe03d2 100644 (file)
@@ -1514,6 +1514,18 @@ fn flat_map<U, F>(self, f: F) -> FlatMap<Self, U, F>
     /// 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:
     ///
     /// ```