How to move Vec<T> into Iterator<Item=T>

Hi, I am kind of stuck with the task of moving a collection into an iterator of its values, but where each item itself can be moved out of the iterator.

Usually I use .into_iter() to move an iterator out of a collection of T, but this creates an Iterator<Item=&'a T>, so I can't move out of the iterator when processing it partially, i.e. without consuming the entire iterator, because the values are owned by the iterator, if I understand correctly.

So basically I am looking for a way to turn a Vec<T> into an Iterator<Item=T> without cloning, but by moving the values into the iterator, and moving them out of the iterator when they are consumed.

Does this make sense? Is it possible?

The .into_iter() method on a Vec<T> does produce an iterator of item type T.

1 Like

Are you sure that you have a Vec<T> instead of a &Vec<T> or &[T]? Calling into_iter on the latter two will give you an Iterator<Item = &T>

2 Likes

Oh, whoops, I was reading the documentation for IntoIterator for &Vec<T> :flushed:

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.