Skip to content

Incorrect '&'-reference removal #140166

Closed
@adi-joshi

Description

@adi-joshi

Code

fn print_board(ttt_array: &[[char; 3]; 3]) {
    println!("{:?}", ttt_array.into_iter().flat_map(|x| &x.into_iter().collect::<String>().chars()));
}

Current output

error[E0277]: `&Chars<'_>` is not an iterator
  --> src/main.rs:34:44
   |
34 |     println!("{:?}", ttt_array.into_iter().flat_map(|x| &x.into_iter().collect::<String>().chars()));
   |                                            ^^^^^^^^ `&Chars<'_>` is not an iterator
   |
   = help: the trait `Iterator` is not implemented for `&Chars<'_>`
   = help: the trait `Iterator` is implemented for `Chars<'_>`
   = note: `Iterator` is implemented for `&mut Chars<'_>`, but not for `&Chars<'_>`
   = note: required for `&Chars<'_>` to implement `IntoIterator`
note: required by a bound in `flat_map`
  --> /rustc/05f9846f893b09a1be1fc8560e33fc3c815cfecb/library/core/src/iter/traits/iterator.rs:1430:5

error[E0277]: `&Chars<'_>` is not an iterator
  --> src/main.rs:34:22
   |
34 |     println!("{:?}", ttt_array.into_iter().flat_map(|x| &x.into_iter().collect::<String>().chars()));
   |               ----   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `&Chars<'_>` is not an iterator
   |               |
   |               required by a bound introduced by this call
   |
   = help: the trait `Iterator` is not implemented for `&Chars<'_>`
   = note: `Iterator` is implemented for `&mut Chars<'_>`, but not for `&Chars<'_>`
   = note: required for `&Chars<'_>` to implement `IntoIterator`
   = note: required for `FlatMap<std::slice::Iter<'_, [char; 3]>, &Chars<'_>, {closure@src/main.rs:34:53: 34:56}>` to implement `Debug`
note: required by a bound in `core::fmt::rt::Argument::<'_>::new_debug`
  --> /rustc/05f9846f893b09a1be1fc8560e33fc3c815cfecb/library/core/src/fmt/rt.rs:117:5
   = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider removing the leading `&`-reference
   |
34 -     println!("{:?}", ttt_array.into_iter().flat_map(|x| &x.into_iter().collect::<String>().chars()));
34 +     println!("{:?}", tt_array.into_iter().flat_map(|x| &x.into_iter().collect::<String>().chars()));
   |

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

Desired output

Rationale and extra context

The proposed change by the compiler is to remove the leading &-reference by removing the first character of the expression (which is not an & in this case).

I believe the &-reference in this case is from inside the lambda expression (the flat_map(|x| &x.into...) part).

Other cases

Rust Version

$ rustc --version --verbose
rustc 1.86.0 (05f9846f8 2025-03-31)
binary: rustc
commit-hash: 05f9846f893b09a1be1fc8560e33fc3c815cfecb
commit-date: 2025-03-31
host: x86_64-unknown-linux-gnu
release: 1.86.0
LLVM version: 19.1.7

Anything else?

No response

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-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