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

Suggest len instead of count on arrays, slices, or vectors #87302

Closed
teor2345 opened this issue Jul 20, 2021 · 0 comments · Fixed by #87614
Closed

Suggest len instead of count on arrays, slices, or vectors #87302

teor2345 opened this issue Jul 20, 2021 · 0 comments · Fixed by #87614
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@teor2345
Copy link
Contributor

Given the following code:

fn main() {
    let slice = [1,2,3,4];
    let vec = vec![1,2,3,4];
    
    println!("{} {} {}", slice.count(), vec.count(), vec.as_slice().count());
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=8bd1115b12adc0eb5feb16bbb63a06cb

The current output is:

error[E0599]: the method `count` exists for array `[{integer}; 4]`, but its trait bounds were not satisfied
 --> src/main.rs:5:32
  |
5 |     println!("{} {} {}", slice.count(), vec.count(), vec.as_slice().count());
  |                                ^^^^^ method cannot be called on `[{integer}; 4]` due to unsatisfied trait bounds
  |
  = note: the following trait bounds were not satisfied:
          `[{integer}; 4]: Iterator`
          which is required by `&mut [{integer}; 4]: Iterator`
          `[{integer}]: Iterator`
          which is required by `&mut [{integer}]: Iterator`

(repeated 3 times with slight variations)

Ideally the output should:

  • suggest using slice.len() instead of slice.count()

This is a common error, particularly for new users, and when switching between iterators and vectors/slices/arrays.

@teor2345 teor2345 added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 20, 2021
@estebank estebank added D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-papercut Diagnostics: An error or lint that needs small tweaks. labels Jul 22, 2021
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 7, 2021
…rk-Simulacrum

Recommend fix `count()` -> `len()` on slices

Fixes rust-lang#87302
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 7, 2021
…rk-Simulacrum

Recommend fix `count()` -> `len()` on slices

Fixes rust-lang#87302
@bors bors closed this as completed in 6a17ee6 Dec 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants