Rust by example Iterator::any example code based on pre Rust 2021


I wanted to make aware that there is discrepancy between Iterator::any - Rust By Example build output and that of the Rust Playground. Specifically, the example works as described in the build output but these lines:

let array2 = [4, 5, 6];
println!("2 in array2: {}", array2.into_iter().any(|x| x == 2));

Unmodified, throw this error (abbreviated) in rust playground:

Compiling playground v0.0.1 (/playground)
error[E0277]: can't compare `&{integer}` with `{integer}`
 --> src/
4 |     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}`

Adding an & before the x between the pipes prints this warning (abbreviated), explaining the situation:

Compiling playground v0.0.1 (/playground)
warning: this method call resolves to `<&[T; N] as IntoIterator>::into_iter` (due to backwards compatibility), but will resolve to <[T; N] as IntoIterator>::into_iter in Rust 2021
 --> src/
4 |     println!("2 in array2: {}", array2.into_iter().any(|&x| x == 2));
  |                                        ^^^^^^^^^
  = warning: this changes meaning in Rust 2021
  = note: for more information, see <>
  = note: `#[warn(array_into_iter)]` on by default
help: use `.iter()` instead of `.into_iter()` to avoid ambiguity

The examples on this page will work fine if you compile Rust code with Edition 2021, which is the default setting in Rust playground.

If you have edition set to something else, please change it to 2021. The edition can be changed using second ellipsis button.

For backwards compatibility reasons, .into_iter() when used on arrays means .iter() in Editions 2015 and 2018. This avoids breaking old programs.

Hello @xfix

That is odd, the setting was indeed 2018. I see the same problem on rustc 1.66.0 (69f9c33d7 2022-12-12) (Arch Linux rust 1:1.66.0-1). Would you by any chance know if the edition has to be set for my compiler somewhere? I am slightly confused by this edition setting as I was assuming that compiler version 1.66.0 is the only thing that mattered?

Thanks for the clarification so far.

If you set the edition explicitly in Cargo.toml, it will override the default. See eg. the docs

Right, I see the problem. If you would be using Cargo then a project started with cargo new would have edition = "2021". However, because you are using rustc (by this point of Rust by Example Cargo wasn't introduced) you need to explicitly specify edition 2021 using --edition 2021.

That appears to be a bug, so I reported it on Iterator::any example uses Edition 2021 features without saying how to enable Edition 2021 in Rustc · Issue #1657 · rust-lang/rust-by-example · GitHub.

Hello @xfix

Thanks, confirming that --edition 2021 fixes the problem! I also went into Rust playground directly, via searching for it, rather than opening a link someone else posted, i think that might have been the issue there.

I will look into Cargo. Arch has even a wiki entry for this: Rust - ArchWiki encouraging to read rusts documentation. Just using rustc seems shorter though, maybe I continue with that until I run into issues and then use the command line option for that case, will have to see.

Thank you @xfix and @H2CO3 for helping.

Using rustc without Cargo (or any other build system) is only "shorter" if you are writing very simple programs that have absolutely no third-party dependencies. I'm not saying you shouldn't continue as you are, but you should expect to need Cargo very soon once you get into writing more practical programs.


Got it @kpreid, thanks for the hint. If nothing else, I will definitely look into it after i am done or got tired working through Rust by example. Running cargo without options does give me the impression it is quite a bit more than just a simple rust wrapper... Looking forward to learning about it.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.