skip to content

In Rust 1.99, `readings.into_iter()` on a `[u8; 3]` yields `&u8` in a 2018 crate but `u8` in a 2021 crate; why wasn't the impl simply made 2021-only?

level: seniorimportance: should knowfreq 26%

answer

  1. one program, many editions
  2. impls are global facts
  3. added for everyone in 1.53
  4. only method-call lookup differs

basics

~20 s

Editions mix in one program, and a trait impl is global, so an impl cannot exist for 2021 crates only. IntoIterator for arrays was added on all editions in 1.53; only 2015 and 2018 method-call syntax still resolves to the reference impl.

solid answer

~40 s

A trait impl is a single fact shared by every crate in the program and checked by coherence, and crates on different editions link together, so an impl cannot exist in one edition and not another. Rust 1.53 therefore added `IntoIterator` for arrays on every edition. Adding it plainly would have changed the meaning of existing `array.into_iter()` calls, which auto-referenced to `(&array).into_iter()` and yielded references, so the compiler keeps that old method-call resolution in 2015 and 2018 crates and warns with the `array_into_iter` lint. Only the `.into_iter()` method-call syntax is special-cased: `for x in readings` and `IntoIterator::into_iter(readings)` yield values on every edition. The 2021 edition dropped the special case, and `cargo fix --edition` rewrites such calls to `.iter()`.

code

rust · 17 lines
rust
fn kind<T>(_: &T) -> &'static str {
    std::any::type_name::<T>()
}

fn main() {
    let readings = [7u8, 8, 9];
    for r in readings.into_iter() {
        println!("method call yields {}", kind(&r));
        break;
    }
    for r in readings {
        println!("for loop yields {}", kind(&r));
        break;
    }
    let first = IntoIterator::into_iter(readings).next().unwrap();
    println!("path call yields {}", kind(&first));
}

go deeper

for a junior

Recall that array.into_iter() yields references on 2015 and 2018 and values on 2021, while a for loop over an array yields values on every edition.

for a middle

Explain auto-referencing: before 1.53 the method call found the impl for &[T; N], so adding a by-value impl would have changed existing code.

for a senior

Tie it to the edition model: impls are program-wide and editions are mixed, so only per-crate method-call resolution could vary, flagged by array_into_iter and rewritten by cargo fix.

for a principal

Use the case to show what editions can and cannot change, and why std additions that alter method resolution need this kind of staged special case.

## What you observe The same line behaves differently by edition. Compiled with rustc 1.99.1: | Expression on `[u8; 3]` | 2015 / 2018 | 2021 / 2024 | |---|---|---| | `readings.into_iter()` | yields `&u8`, with an `array_into_iter` warning | yields `u8` | | `for r in readings` | yields `u8` | yields `u8` | | `IntoIterator::into_iter(readings)` | yields `u8` | yields `u8` | Only one syntax differs, and that is the clue to how editions are built. ## Why the impl could not be per edition Editions are a property of a crate's **source**, and crates on different editions link into one program. A **trait impl**, by contrast, is a property of the **program**: coherence guarantees there is at most one impl of a trait for a type, visible identically to every crate. If `impl IntoIterator for [T; N]` existed only for 2021 crates, a 2021 crate and a 2018 crate in one build could disagree about whether arrays implement the trait — what a generic function bounded by `T: IntoIterator` accepts would depend on who was asking. The edition guide puts it in one sentence: an impl cannot exist in one edition and not in another, since editions can be mixed. So the impl had to be added for everyone, which happened in **Rust 1.53**. ## Why adding it for everyone was still a problem Before 1.53, only `&[T; N]` and `&mut [T; N]` implemented `IntoIterator`. A method call `readings.into_iter()` still compiled, because method lookup **auto-references** the receiver and found the impl for `&[T; N]`: the call meant `(&readings).into_iter()` and yielded references. Adding the by-value impl changes what method lookup finds first, so every existing `array.into_iter()` would silently switch from references to values. Adding an impl is usually considered a minor, acceptable change, but here too much code depended on it. ## The compromise 1. The impl exists on **all editions** from 1.53. 2. In **2015 and 2018** crates, the compiler resolves the *method-call syntax* `array.into_iter()` to `(&array).into_iter()` as if the by-value impl were not there. 3. The `array_into_iter` lint, warn by default on all editions since 1.41 and part of the `rust-2021-compatibility` group, flags each such call: "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". 4. In **2021**, the special case is gone. `cargo fix --edition` keeps the old meaning by rewriting `array.into_iter()` to `array.iter()`, which yields references on every edition. This is the edition mechanism at its narrowest: the edition changes **how one crate's source is resolved**, never what the shared library contains. ## Migrating such a call by hand - Keep the old meaning on any edition with `readings.iter()`, which yields `&u8` everywhere; this is the rewrite `cargo fix --edition` makes. - Take values on any edition since 1.53 with `for r in readings` or the fully qualified `IntoIterator::into_iter(readings)`. - Once the crate is on 2021, `readings.into_iter()` can be used for by-value iteration, and the edition guide lists turning `IntoIterator::into_iter(readings)` back into the method call as an optional clean-up. - Code that relied on the references, for example by writing `*r` in a closure, fails with E0614 ("type `u8` cannot be dereferenced") if a call is left unfixed when the edition changes. ## The same pattern elsewhere - **The 2021 prelude.** Adding `TryInto` to everyone's prelude would make calls ambiguous where a crate's own trait has a `try_into` method (error E0034, "multiple applicable items in scope"). The prelude is per-crate name resolution, so it *could* be edition-specific: 2021 crates import `std::prelude::rust_2021`, older crates do not. - **Boxed slices in 2024.** `Box<[T]>` gained a by-value `IntoIterator` on all editions, with the same method-call special case before 2024 and a `boxed_slice_into_iter` lint; the details belong to the 2024 lesson. ## What this tells an interviewer - You know editions are mixed in one build, and what follows from it. - You can separate **per-crate** things editions can change (syntax, keywords, prelude, method-call resolution in narrow cases) from **program-wide** things they cannot (impls, std's contents, coherence). - You can read the lint and the `cargo fix` rewrite as consequences of that design, not as arbitrary quirks. How `for` loops and `IntoIterator` work in general is covered with iterators.

  • Why does `for r in readings` yield `u8` even in a 2018 crate?
    A `for` loop calls `IntoIterator::into_iter(readings)` through the trait path, not through method-call syntax, so auto-referencing never happens and the by-value impl added in 1.53 is chosen. The backwards-compatibility special case covers only `readings.into_iter()` written as a method call in 2015 and 2018 crates.
  • A 2018 crate defines its own trait with a `try_into` method; what breaks when it moves to 2021, and why could that change be per edition?
    The 2021 prelude imports `TryInto`, so `value.try_into()` can become ambiguous and fail with E0034, "multiple applicable items in scope"; fully qualified syntax fixes it, and the `rust_2021_prelude_collisions` lint lets `cargo fix --edition` rewrite it. The prelude is name resolution inside one crate, so editions can change it without affecting other crates.

  • Arrays only implement IntoIterator on the 2021 edition and later.
  • for x in array yields references on the 2018 edition.
  • An edition could have added the impl just for 2021 crates.
  • The 2018 behaviour comes from a separate 2018 standard library.
  • cargo fix --edition rewrites array.into_iter() to by-value iteration.

Assumes rustc 1.99.1; IntoIterator for arrays exists on every edition since Rust 1.53, and the 2021 edition (1.56) removed the method-call special case.