rust-lang / rust-lang/rust-clippy

suggest iterator.take() instead of .enumerate() with comparison and break

Open
#3,405 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint C-enhancement L-complexity
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

    let x = vec![0,1,2,3,4,5,6,7,8];

    let limit: usize = 4;

    for (count,data) in x.into_iter().enumerate() {
        if count == limit {
            break
        }
        println!("{}", data);
    } 

Instead, iterator.take() could be used which looks much nicer

    let x = vec![0,1,2,3,4,5,6,7,8];
    let limit: usize = 4;
    x.into_iter().take(limit).for_each(|data| println!("{}", data));

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.

Research direction

Use the issue's two Rust examples as the behavioral specification, then locate the existing Clippy iterator-lint implementations and their tests. The work is done when the pattern using enumerate(), a comparison, and break receives a take() suggestion, with tests covering the shown limit behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.