skip to content

In Rust 2024, why does `ports.into_iter()` on a `Box<[u16]>` yield `u16` instead of `&u16`, and why did cargo fix rewrite it?

level: middleimportance: nice to knowfreq 15%

answer

  1. method call versus for loop
  2. an impl added in Rust 1.80
  3. resolution hidden before 2024
  4. the boxed_slice_into_iter lint

basics

~20 s

Rust 1.80 gave Box<[T]> a by-value IntoIterator, but before 2024 the method call boxed.into_iter() still resolved to a by-reference iterator. On 2024 it consumes the box and yields T, so cargo fix writes .iter() to keep references.

solid answer

~40 s

Rust 1.80 added `IntoIterator` for `Box<[T]>` in every edition, so `for p in ports` iterates by value everywhere. A method call `ports.into_iter()` was treated differently: before 2024 it still resolved to `<&Box<[T]> as IntoIterator>::into_iter` and yielded `&u16`, purely for backward compatibility. On the 2024 edition the same call resolves to the by-value impl, so it moves the box and yields `u16`. Code written for the old meaning then fails: a closure doing `**p` gets E0614, a later `ports.len()` gets E0382, and a `Vec<&u16>` annotation gets E0277. The `boxed_slice_into_iter` lint warns on every edition, and `cargo fix --edition` rewrites the call to `.iter()` to keep references; if owned items were what you wanted, revert that and keep `into_iter()`.

code

rust · 5 lines
rust
fn main() {
    let ports: Box<[u16]> = vec![8080, 8443].into_boxed_slice();
    let open: Vec<_> = ports.into_iter().filter(|p| **p != 8443).collect();
    println!("{open:?} of {}", ports.len());
}

go deeper

for a junior

Recall that on Rust 2024 calling into_iter() on a Box<[T]> consumes the box and yields owned elements, while iter() still borrows them.

for a middle

Explain the split between the for loop and method-call syntax, why the by-value impl from Rust 1.80 stayed hidden from method calls before 2024, and which errors old code hits.

for a senior

Show how you review each cargo fix rewrite to iter(): keep it where the box is reused, revert it where owned elements were the point, and read the lint before switching editions.

for a principal

Treat this as a template for how Rust ships a library change that alters meaning: add the impl for everyone, hide it behind the edition, warn on all editions, and let each team choose.

## Two ways to ask a boxed slice for an iterator A **boxed slice**, `Box<[T]>`, is a heap allocation holding a fixed number of elements, often produced by `Vec::into_boxed_slice`. Rust code asks it for an iterator in one of two ways: - a `for` loop, `for p in ports { ... }`, which calls `IntoIterator::into_iter(ports)` on the value itself; - a **method call**, `ports.into_iter()`, which goes through method resolution and may auto-reference or auto-dereference the receiver until it finds a matching method. Until Rust 1.80 `Box<[T]>` had no `IntoIterator` impl of its own. The `for` loop did not compile, because `for` does not auto-dereference, while the method call auto-dereferenced to the slice and found `<&[T] as IntoIterator>::into_iter`, which yields `&T`. Plenty of code came to rely on `.into_iter()` meaning "iterate by reference" on a boxed slice. ## What Rust 1.80 and the 2024 edition changed Rust 1.80 added `impl IntoIterator for Box<[T]>`, yielding owned `T`, in every edition. Letting method calls find it at once would have silently changed the item type of existing code, so method-call syntax on editions before 2024 skips the new impl and keeps resolving to the reference iterator. The 2024 edition removes that special case. | Syntax | Rust 1.80+ on editions 2015-2021 | Rust 1.80+ on edition 2024 | |---|---|---| | `for p in ports` | `u16`, consumes the box | `u16`, consumes the box | | `ports.into_iter()` | `&u16`, borrows | `u16`, consumes the box | | `ports.iter()` | `&u16`, borrows | `&u16`, borrows | | `IntoIterator::into_iter(ports)` | `u16`, consumes the box | `u16`, consumes the box | ## How old code breaks on 2024 Code written for the reference meaning usually fails to compile instead of misbehaving, because the item type is visible to the type checker. rustc 1.99 on edition 2024 reports, for example: 1. **E0614**, "type `u16` cannot be dereferenced", where a closure wrote `**p` expecting `&&u16`; 2. **E0382**, "borrow of moved value: `ports`", where the box is used after the loop, since it has now been consumed; 3. **E0277**, "a value of type `Vec<&u16>` cannot be built from an iterator over elements of type `u16`", where a collection's type was spelled out. Code that never named the item type and never touched the box again may compile on both editions and simply work on owned values, which is harmless for `Copy` items and a move for others. ## Why an edition carries this change Rust releases add trait impls routinely. This one was different because method resolution would pick the new impl for code that already compiles, silently changing the item type from `&T` to `T` and the box from borrowed to moved. Rust's stability promise rules that out for existing code, so the release added the impl for everyone and the 2024 edition is the opt-in point where method calls start to see it. ## The migration lint and the decision it leaves you The `boxed_slice_into_iter` lint is warn-by-default on every edition, so a crate built with a current compiler has already seen it. Its message reads that the call resolves to `<&Box<[T]> as IntoIterator>::into_iter` for backwards compatibility but will resolve to `<Box<[T]> as IntoIterator>::into_iter` in Rust 2024. It offers two fixes: use `.iter()` to keep references, or drop `.into_iter()` (or call `IntoIterator::into_iter(..)`) to iterate by value. `cargo fix --edition` always applies the first, so the migrated code means exactly what it meant before. Review each rewrite: - keep `.iter()` where the box is used again later, or the elements are expensive to move; - revert to `.into_iter()` where the code only wanted the elements and the box is finished with, since owned items save a dereference or a clone. The same pattern hit arrays in the 2021 edition, covered with by-value iteration; the 2024 change is the boxed-slice version of it. ## Other 2024 rewrites of the same kind Two more 2024 changes are pure name or resolution shifts that `cargo fix --edition` handles mechanically. `gen` is now a reserved keyword, so `fn gen()` becomes `fn r#gen()`, a raw identifier usable on both editions (lint `keyword_idents_2024`). The 2024 prelude adds `Future` and `IntoFuture`, so a call to a method of the same name from another trait, such as `poll`, can resolve differently or become ambiguous; the `rust_2024_prelude_collisions` lint rewrites such calls in fully qualified syntax.

  • Why can `for p in ports` iterate a `Box<[u16]>` by value on the 2021 edition when `ports.into_iter()` cannot?
    The compatibility special case applies only to method-call syntax. A `for` loop calls `IntoIterator::into_iter(ports)` on the box itself and never auto-dereferenced, so before Rust 1.80 it simply did not compile; nobody could depend on a reference meaning. From 1.80 it uses the new by-value impl in every edition.
  • When should you revert cargo fix's `.iter()` rewrite after moving to 2024?
    When the code only needs the elements and the box is not used afterwards. Then `ports.into_iter()`, or a plain `for p in ports`, hands out owned values, which avoids dereferencing `Copy` items and avoids cloning non-`Copy` ones. Keep `.iter()` wherever the box is read again later or must stay intact.

  • boxed.into_iter() yields references on every edition, as it always did
  • Box<[T]> still has no IntoIterator impl of its own
  • cargo fix rewrites the call to iterate by value
  • The 2024 change silently alters results without any compile error or lint

Assumes Rust 1.99; Box<[T]> has implemented IntoIterator since 1.80, and the method call boxed.into_iter() resolves to it only on the 2024 edition (stable since 1.85).