Add result example + rewording

This commit is contained in:
Fabian Hintringer 2022-12-13 09:17:22 +01:00 committed by GitHub
parent 76438d26b1
commit 083560b7d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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