Skip to content

Invalid suggestion for a reference of iterator #127590

Closed
@chenyukang

Description

@chenyukang

Code

fn main() {
    let fields = vec!["a", "b", "c"];
    let variant = vec!["a", "b", "c"];

    for (src, dest) in std::iter::zip(fields.iter(), &variant.iter()) {
        eprintln!("{} {}", src, dest);
    }
}

Current output

error[E0277]: `&std::slice::Iter<'_, &str>` is not an iterator
  --> ./p/iter.rs:5:54
   |
5  |     for (src, dest) in std::iter::zip(fields.iter(), &variant.iter()) {
   |                        --------------                ^^^^^^^^^^^^^^^ `&std::slice::Iter<'_, &str>` is not an iterator
   |                        |
   |                        required by a bound introduced by this call
   |
   = help: the trait `Iterator` is not implemented for `&std::slice::Iter<'_, &str>`, which is required by `&std::slice::Iter<'_, &str>: IntoIterator`
   = note: required for `&std::slice::Iter<'_, &str>` to implement `IntoIterator`
note: required by a bound in `std::iter::zip`
  --> /Users/yukang/rust/library/core/src/iter/adapters/zip.rs:70:8
   |
67 | pub fn zip<A, B>(a: A, b: B) -> Zip<A::IntoIter, B::IntoIter>
   |        --- required by a bound in this function
...
70 |     B: IntoIterator,
   |        ^^^^^^^^^^^^ required by this bound in `zip`
help: consider dereferencing here
   |
5  |     for (src, dest) in std::iter::zip(fields.iter(), *&variant.iter()) {
   |                                                      +
help: consider removing the leading `&`-reference
   |
5  -     for (src, dest) in std::iter::zip(fields.iter(), &variant.iter()) {
5  +     for (src, dest) in std::iter::zip(fields.iter(), variant.iter()) {
   |
help: consider changing this borrow's mutability
   |
5  |     for (src, dest) in std::iter::zip(fields.iter(), &mut variant.iter()) {
   |                                                      ~~~~

error[E0277]: `&std::slice::Iter<'_, &str>` is not an iterator
 --> ./p/iter.rs:5:24
  |
5 |     for (src, dest) in std::iter::zip(fields.iter(), &variant.iter()) {
  |                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `&std::slice::Iter<'_, &str>` is not an iterator
  |
  = help: the trait `Iterator` is not implemented for `&std::slice::Iter<'_, &str>`, which is required by `Zip<std::slice::Iter<'_, &str>, &std::slice::Iter<'_, &str>>: IntoIterator`
  = help: the trait `Iterator` is implemented for `std::slice::Iter<'a, T>`
  = note: `Iterator` is implemented for `&mut std::slice::Iter<'_, &str>`, but not for `&std::slice::Iter<'_, &str>`
  = note: required for `Zip<std::slice::Iter<'_, &str>, &std::slice::Iter<'_, &str>>` to implement `Iterator`
  = note: required for `Zip<std::slice::Iter<'_, &str>, &std::slice::Iter<'_, &str>>` to implement `IntoIterator`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0277`.

Desired output

Remove the first suggestion:


help: consider dereferencing here
   |
5  |     for (src, dest) in std::iter::zip(fields.iter(), *&variant.iter()) {
   |                                                      +

Rationale and extra context

Suggest dereferencing is not right here, if we use *&variant, the code is still not right, and continue to suggest:

help: consider cloning the value if the performance cost is acceptable
  |
5 -     for (src, dest) in std::iter::zip(fields.iter(), *&variant.iter()) {
5 +     for (src, dest) in std::iter::zip(fields.iter(), variant.iter().clone()) {
  |

And add more .clone() it will continue to suggest add .clone() in the next compiling, this is another issue need to be fixed.

help: consider cloning the value if the performance cost is acceptable
  |
5 -     for (src, dest) in std::iter::zip(fields.iter(), *&variant.iter().clone()) {
5 +     for (src, dest) in std::iter::zip(fields.iter(), variant.iter().clone().clone()) {

Other cases

No response

Rust Version

rustc 1.80.0-nightly (8c127df75 2024-05-16)
binary: rustc
commit-hash: 8c127df75fde3d5ad8ef9af664962a7676288b52
commit-date: 2024-05-16
host: aarch64-apple-darwin
release: 1.80.0-nightly
LLVM version: 18.1.4

Anything else?

No response

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions