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


#1

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).


#2

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


#3

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


#4

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…).


#5

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