rust-lang / rust-lang/rust-clippy
`missing_iterator_fold`
Open
@Philippe-Cholet is already working on this.
Since Jan 29, 2024.
A-lint
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
Lints implementations of core::iter::Iterator that do not specialize the fold method.
Advantage
- Methods that consume the entire iterator (
for_each,count, ...) rely onfoldby default and would benefit fromfoldbeing specialized. - Adaptor iterators might use
fold(or related methods) for their own usage and would benefit as well.
Drawbacks
Specialize it may not be faster than the default behavior.
Example
struct Iter(u8);
impl Iterator for Iter {
type Item = u8;
fn next(&mut self) -> Option<Self::Item> {
todo!()
}
}
Could be written as:
struct Iter(u8);
impl Iterator for Iter {
type Item = u8;
fn next(&mut self) -> Option<Self::Item> {
todo!()
}
fn fold<B, F>(self, init: B, f: F) -> B
where
F: FnMut(B, Self::Item) -> B
{
todo!()
}
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.