Repository navigation
Documentation of Iterator flatten() improvement #82687
Description
Activity
@rustbot label T-doc T-libs.
- addedA-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsArea: Documentation for any part of the project, including the compiler, standard library, and toolsT-libs-api[DEPRECATED; DO NOT USE][DEPRECATED; DO NOT USE]
on Mar 2, 2021 - addedA-iteratorsArea: IteratorsArea: IteratorsC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.
on Mar 2, 2021 I'm not sure I like clippy's suggestion, personally, but I think your suggested documentation is reasonable. I would also point out that mapping to
Option/Resultfollowed byflatten(or a similarflat_map) is better written asfilter_map(withok()forResult).I was also confused when I saw the suggestion. But once I realizing what is was doing it sounded in line with things like
unwrap. That does not mean that I like it, but at least people know it is intended that way and not a side-effect.I encountered the same clippy warning when iterating over Results, and got perplexed. With the current state of documentation, I personally perceive the warning as suggestion to write obfuscated code hiding the fact that error checking is missing.
Adding the documentation change suggested by @ralpha would make it much easier to realize what flatten() does. Maybe it might be made even more clear how it works if phrasing the first line something like this:
Flattening works on all types implementing Iterator, like Option and Result:
Me too am no longer new to Rust, and thought I had a proficient understanding of flatten() after having used it to unroll nested arrays. Likely I'll not be the last person to be surprised it can also be used to discard Err values.
@rustbot claim
- added a commit that references this issue
on Jan 9, 2023 - added a commit that references this issue
on Jan 9, 2023
While linting my code I came across
manual_flattenwhich was triggered in my code.This suggests the change from:
Into:
Both are valid code with same result.
But the rust docs on
flatten()does not state that this is expected behavior. Yes, this is stated here.But this was not strait forward (in my opinion) to find. It took me writing a whole bug report and looking a bunch of thing up to figure this out. (and I'm not even new to Rust anymore)
I would suggest adding some example code to make his explicit. Here is a suggestion:
(after the "Mapping and then flattening:
<code>...</code>" section)(or something similar)
This makes both this behavior cleared, easier to find and exposes people to code like this so they are less surprised if clippy starts warning them.
It looks like this behavior might be more common in the future too.
rust-lang/rust-clippy#6061
rust-lang/rust-clippy#6676