Pre-RFC: make `&[T]` implement `IntoIterator<Item = T>` (where `T: Copy`)

Have we ever thought about adding making &[T] implement IntoIterator<Item = T> where T: Copy (similarly to how extend works)? Right now it’ quite annoying to take a function over slices:

fn foo(x: &[u32]) { .. }

and make it also accept iterators:

fn foo(x: impl IntoIterator<Item = u32>) { .. }

since all of the callers must become foo(x.iter().cloned()). At least in a lot of the code bases I work on, this comes up most often with small, copy types, and it seems at least plausible that we might have &[T] implement IntoIterator<Item = T> when T: Copy (not sure if it actually works).

3 Likes

How would that fit with the existing IntoIterator<Item = &'a T>?

1 Like

oh yeah of course it won’t work, what am I saying. Annoying.

1 Like

Could introduce a new IntoIteratorOf<T> trait that takes an input type parameter, and have it be used for functions, while for loops would continue using IntoIterator so inference works, and have a blanket impl of it for IntoIterator (not sure if that works without specialization…).

1 Like

Related: https://github.com/rust-lang/rfcs/pull/2185

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.