rust-lang / rust-by-example

Learn Rust with examples (Live code editor included)
https://doc.rust-lang.org/stable/rust-by-example/
Apache License 2.0
6.97k stars 1.34k forks source link

into_iter() for arrays example is not valid for more recent rust compilers #1604

Open augustfengd opened 2 years ago

augustfengd commented 2 years ago

Hello!

When I copy the snippet in the section 9.2.6.1 and try compile locally, I get an error:

// rustc learn-1.rs
error[E0277]: can't compare `&{integer}` with `{integer}`
  --> learn-1.rs:16:62
   |
16 |     println!("2 in array2: {}", array2.into_iter().any(|x| x == 2));
   |                                                              ^^ no implementation for `&{integer} == {integer}`
   |
   = help: the trait `PartialEq<{integer}>` is not implemented for `&{integer}`
   = help: the following other types implement trait `PartialEq<Rhs>`:
             f32
             f64
             i128
             i16
             i32
             i64
             i8
             isize
           and 6 others

error: aborting due to previous error

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

My rust compiler version:

rustc --version
rustc 1.63.0 (4b91a6ea7 2022-08-08)

The web application is still compiling it correctly though!

Spoonbender commented 1 year ago

This works on rustc 1.64.0 (a55dd71d5 2022-09-19). I also tried going back to 1.63.0 and was unable to reproduce the issue in that version either. @augustfengd can you re-check and also specify further details (e.g. which O/S are you using)?

augustfengd commented 1 year ago

Hey @Spoonbender, sorry for the (super) late reply. I'm on a new machine now with rustc 1.67.0 (fc594f156 2023-01-24) + WSL2 and I confirm the issue persists.

However, it's fine when invoking with cargo run.

Update: rustc --edition 2021 <file> works!

iamtroy412 commented 1 year ago

I was running into the same thing and can confirm that rustc --edition 2021 <file> works for me too. Thanks!

lluisalemanypuig commented 4 months ago

I also ran into the same problem. My rustc --version is rustc 1.78.0 (9b00956e5 2024-04-29). Error message:

error[E0277]: can't compare `&{integer}` with `{integer}`
  --> 9_closures_in_std.rs:25:60
   |
25 |     println!("2 in array2: {}", array2.into_iter().any(|x | x == 2));
   |                                                               ^^ no implementation for `&{integer} == {integer}`
   |
   = help: the trait `PartialEq<{integer}>` is not implemented for `&{integer}`
help: consider dereferencing here
   |
25 |     println!("2 in array2: {}", array2.into_iter().any(|x | *x == 2));
   |                                                             +

error: aborting due to 1 previous error

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

I can confirm that rustc --edition 2021 <file> works fine.