rust-lang / rust-lang/rust-clippy

`missing_iterator_fold`

Open
#12,209 3 comments 1 reaction 1 assignee View on GitHub

@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 on fold by default and would benefit from fold being 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.