Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example for iterator_flatten #105034

Merged
merged 3 commits into from
Jan 10, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions library/core/src/iter/traits/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1495,6 +1495,14 @@ pub trait Iterator {
/// assert_eq!(merged, "alphabetagamma");
/// ```
///
/// Flattening also works on other types like Option and Result:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not really specific to Option or Result though. It's that Flatten doesn't require Item: Iterator, it only needs Item: IntoIterator and Option/Result are types that implement that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think i do not really understand what you mean, should we not add this example should i change it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pointing out that "types like Option and Result" is very vague. It doesn't tell the reader which those types have in common so they can be used with Flatten. I don't have a particular improvement in mind, I was just pointing out the vagueness and how it actually works.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like we already document that "iterator of iterators or an iterator of things that can be turned into iterators" above -- I'm not sure another example is all that useful. I think we have enough examples personally for flatten.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is okay, although its placement is interrupting the "you can also rewrite this" example below, so we should move this down. For wording, perhaps:

Suggested change
/// Flattening also works on other types like Option and Result:
/// Flattening works on any `IntoIterator` type, including `Option` and `Result`:

Although we're not showing Result, so maybe do so too, or remove that part.

///
/// ```
/// 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:
///
Expand Down