pub trait IntoIterator {
type Item;
type IntoIter: Iterator
where
<Self::IntoIter as Iterator>::Item == Self::Item;
fn into_iter(self) -> Self::IntoIter;
}
mtls
only.Expand description
Conversion into an Iterator
.
By implementing IntoIterator
for a type, you define how it will be
converted to an iterator. This is common for types which describe a
collection of some kind.
One benefit of implementing IntoIterator
is that your type will work
with Rust’s for
loop syntax.
See also: FromIterator
.
Examples
Basic usage:
let v = [1, 2, 3];
let mut iter = v.into_iter();
assert_eq!(Some(1), iter.next());
assert_eq!(Some(2), iter.next());
assert_eq!(Some(3), iter.next());
assert_eq!(None, iter.next());
Implementing IntoIterator
for your type:
// A sample collection, that's just a wrapper over Vec<T>
#[derive(Debug)]
struct MyCollection(Vec<i32>);
// Let's give it some methods so we can create one and add things
// to it.
impl MyCollection {
fn new() -> MyCollection {
MyCollection(Vec::new())
}
fn add(&mut self, elem: i32) {
self.0.push(elem);
}
}
// and we'll implement IntoIterator
impl IntoIterator for MyCollection {
type Item = i32;
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
// Now we can make a new collection...
let mut c = MyCollection::new();
// ... add some stuff to it ...
c.add(0);
c.add(1);
c.add(2);
// ... and then turn it into an Iterator:
for (i, n) in c.into_iter().enumerate() {
assert_eq!(i as i32, n);
}
It is common to use IntoIterator
as a trait bound. This allows
the input collection type to change, so long as it is still an
iterator. Additional bounds can be specified by restricting on
Item
:
fn collect_as_strings<T>(collection: T) -> Vec<String>
where
T: IntoIterator,
T::Item: std::fmt::Debug,
{
collection
.into_iter()
.map(|item| format!("{item:?}"))
.collect()
}
Required Associated Types
Required Methods
Creates an iterator from a value.
See the module-level documentation for more.
Examples
Basic usage:
let v = [1, 2, 3];
let mut iter = v.into_iter();
assert_eq!(Some(1), iter.next());
assert_eq!(Some(2), iter.next());
assert_eq!(Some(3), iter.next());
assert_eq!(None, iter.next());
Implementations on Foreign Types
1.1.0 · sourceimpl<'a, T> IntoIterator for &'a Receiver<T>
impl<'a, T> IntoIterator for &'a Receiver<T>
1.6.0 · sourceimpl<'a> IntoIterator for &'a PathBuf
impl<'a> IntoIterator for &'a PathBuf
1.10.0 · sourceimpl<'a> IntoIterator for &'a UnixListener
impl<'a> IntoIterator for &'a UnixListener
1.6.0 · sourceimpl<'a> IntoIterator for &'a Path
impl<'a> IntoIterator for &'a Path
1.1.0 · sourceimpl<T> IntoIterator for Receiver<T>
impl<T> IntoIterator for Receiver<T>
sourceimpl<'a, T> IntoIterator for &'a [T]
impl<'a, T> IntoIterator for &'a [T]
1.53.0 · sourceimpl<T, const N: usize> IntoIterator for [T; N]
impl<T, const N: usize> IntoIterator for [T; N]
sourcefn into_iter(self) -> <[T; N] as IntoIterator>::IntoIter
fn into_iter(self) -> <[T; N] as IntoIterator>::IntoIter
Creates a consuming iterator, that is, one that moves each value out of
the array (from start to end). The array cannot be used after calling
this unless T
implements Copy
, so the whole array is copied.
Arrays have special behavior when calling .into_iter()
prior to the
2021 edition – see the array Editions section for more information.
type Item = T
type IntoIter = IntoIter<T, N>
sourceimpl<'a, T, const N: usize> IntoIterator for &'a mut [T; N]
impl<'a, T, const N: usize> IntoIterator for &'a mut [T; N]
sourceimpl<'a, T, const N: usize> IntoIterator for &'a [T; N]
impl<'a, T, const N: usize> IntoIterator for &'a [T; N]
sourceimpl<'a, T> IntoIterator for &'a mut [T]
impl<'a, T> IntoIterator for &'a mut [T]
impl<Fut> IntoIterator for FuturesUnordered<Fut> where
Fut: Unpin,
impl<Fut> IntoIterator for FuturesUnordered<Fut> where
Fut: Unpin,
type Item = Fut
type IntoIter = IntoIter<Fut>
fn into_iter(self) -> <FuturesUnordered<Fut> as IntoIterator>::IntoIter
impl<'a, Fut> IntoIterator for &'a FuturesUnordered<Fut> where
Fut: Unpin,
impl<'a, Fut> IntoIterator for &'a FuturesUnordered<Fut> where
Fut: Unpin,
impl<'a, St> IntoIterator for &'a mut SelectAll<St> where
St: Stream + Unpin,
impl<'a, St> IntoIterator for &'a mut SelectAll<St> where
St: Stream + Unpin,
impl<'a, St> IntoIterator for &'a SelectAll<St> where
St: Stream + Unpin,
impl<'a, St> IntoIterator for &'a SelectAll<St> where
St: Stream + Unpin,
impl<St> IntoIterator for SelectAll<St> where
St: Stream + Unpin,
impl<St> IntoIterator for SelectAll<St> where
St: Stream + Unpin,
type Item = St
type IntoIter = IntoIter<St>
fn into_iter(self) -> <SelectAll<St> as IntoIterator>::IntoIter
impl<'a, Fut> IntoIterator for &'a mut FuturesUnordered<Fut> where
Fut: Unpin,
impl<'a, Fut> IntoIterator for &'a mut FuturesUnordered<Fut> where
Fut: Unpin,
impl<'a, T> IntoIterator for &'a mut Slab<T>
impl<'a, T> IntoIterator for &'a mut Slab<T>
impl<T> IntoIterator for Slab<T>
impl<T> IntoIterator for Slab<T>
impl<'a, T> IntoIterator for &'a Slab<T>
impl<'a, T> IntoIterator for &'a Slab<T>
impl<'a> IntoIterator for &'a Events
impl<'a> IntoIterator for &'a Events
type Item = &'a Event
type IntoIter = Iter<'a>
fn into_iter(self) -> <&'a Events as IntoIterator>::IntoIter
sourceimpl<T, U> IntoIterator for Chain<T, U> where
T: Buf,
U: Buf,
impl<T, U> IntoIterator for Chain<T, U> where
T: Buf,
U: Buf,
sourceimpl<'a> IntoIterator for &'a BytesMut
impl<'a> IntoIterator for &'a BytesMut
sourceimpl IntoIterator for BytesMut
impl IntoIterator for BytesMut
sourceimpl IntoIterator for Error
impl IntoIterator for Error
impl<A> IntoIterator for SmallVec<A> where
A: Array,
impl<A> IntoIterator for SmallVec<A> where
A: Array,
type IntoIter = IntoIter<A>
type Item = <A as Array>::Item
fn into_iter(self) -> <SmallVec<A> as IntoIterator>::IntoIter
impl<'a, A> IntoIterator for &'a SmallVec<A> where
A: Array,
impl<'a, A> IntoIterator for &'a SmallVec<A> where
A: Array,
impl<'a, A> IntoIterator for &'a mut SmallVec<A> where
A: Array,
impl<'a, A> IntoIterator for &'a mut SmallVec<A> where
A: Array,
sourceimpl IntoIterator for Map<String, Value>
impl IntoIterator for Map<String, Value>
sourceimpl<'a> IntoIterator for &'a mut Map<String, Value>
impl<'a> IntoIterator for &'a mut Map<String, Value>
sourceimpl<'a> IntoIterator for &'a Map<String, Value>
impl<'a> IntoIterator for &'a Map<String, Value>
sourceimpl<'a, K, V, S> IntoIterator for &'a IndexMap<K, V, S>
impl<'a, K, V, S> IntoIterator for &'a IndexMap<K, V, S>
sourceimpl<'a, K, V, S> IntoIterator for &'a mut IndexMap<K, V, S>
impl<'a, K, V, S> IntoIterator for &'a mut IndexMap<K, V, S>
sourceimpl<K, V, S> IntoIterator for IndexMap<K, V, S>
impl<K, V, S> IntoIterator for IndexMap<K, V, S>
sourceimpl<T, S> IntoIterator for IndexSet<T, S>
impl<T, S> IntoIterator for IndexSet<T, S>
sourceimpl<'a, T, S> IntoIterator for &'a IndexSet<T, S>
impl<'a, T, S> IntoIterator for &'a IndexSet<T, S>
impl<T, A> IntoIterator for RawTable<T, A> where
A: Allocator + Clone,
impl<T, A> IntoIterator for RawTable<T, A> where
A: Allocator + Clone,
impl<'a, T, S, A> IntoIterator for &'a HashSet<T, S, A> where
A: Allocator + Clone,
impl<'a, T, S, A> IntoIterator for &'a HashSet<T, S, A> where
A: Allocator + Clone,
impl<'a, K, V, S, A> IntoIterator for &'a HashMap<K, V, S, A> where
A: Allocator + Clone,
impl<'a, K, V, S, A> IntoIterator for &'a HashMap<K, V, S, A> where
A: Allocator + Clone,
impl<K, V, S, A> IntoIterator for HashMap<K, V, S, A> where
A: Allocator + Clone,
impl<K, V, S, A> IntoIterator for HashMap<K, V, S, A> where
A: Allocator + Clone,
fn into_iter(self) -> IntoIter<K, V, A>ⓘNotable traits for IntoIter<K, V, A>impl<K, V, A> Iterator for IntoIter<K, V, A> where
A: Allocator + Clone, type Item = (K, V);
fn into_iter(self) -> IntoIter<K, V, A>ⓘNotable traits for IntoIter<K, V, A>impl<K, V, A> Iterator for IntoIter<K, V, A> where
A: Allocator + Clone, type Item = (K, V);
A: Allocator + Clone, type Item = (K, V);
Creates a consuming iterator, that is, one that moves each key-value pair out of the map in arbitrary order. The map cannot be used after calling this.
Examples
use hashbrown::HashMap;
let mut map = HashMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);
// Not possible with .iter()
let vec: Vec<(&str, i32)> = map.into_iter().collect();
type Item = (K, V)
type IntoIter = IntoIter<K, V, A>
impl<'a, K, V, S, A> IntoIterator for &'a mut HashMap<K, V, S, A> where
A: Allocator + Clone,
impl<'a, K, V, S, A> IntoIterator for &'a mut HashMap<K, V, S, A> where
A: Allocator + Clone,
impl<T, S, A> IntoIterator for HashSet<T, S, A> where
A: Allocator + Clone,
impl<T, S, A> IntoIterator for HashSet<T, S, A> where
A: Allocator + Clone,
fn into_iter(self) -> IntoIter<T, A>ⓘNotable traits for IntoIter<K, A>impl<K, A> Iterator for IntoIter<K, A> where
A: Allocator + Clone, type Item = K;
fn into_iter(self) -> IntoIter<T, A>ⓘNotable traits for IntoIter<K, A>impl<K, A> Iterator for IntoIter<K, A> where
A: Allocator + Clone, type Item = K;
A: Allocator + Clone, type Item = K;
Creates a consuming iterator, that is, one that moves each value out of the set in arbitrary order. The set cannot be used after calling this.
Examples
use hashbrown::HashSet;
let mut set = HashSet::new();
set.insert("a".to_string());
set.insert("b".to_string());
// Not possible to collect to a Vec<String> with a regular `.iter()`.
let v: Vec<String> = set.into_iter().collect();
// Will print in an arbitrary order.
for x in &v {
println!("{}", x);
}
type Item = T
type IntoIter = IntoIter<T, A>
impl<'a, K, V, S> IntoIterator for &'a AHashMap<K, V, S>
impl<'a, K, V, S> IntoIterator for &'a AHashMap<K, V, S>
impl<'a, K, V, S> IntoIterator for &'a mut AHashMap<K, V, S>
impl<'a, K, V, S> IntoIterator for &'a mut AHashMap<K, V, S>
impl<K, V, S> IntoIterator for AHashMap<K, V, S>
impl<K, V, S> IntoIterator for AHashMap<K, V, S>
impl<T, S> IntoIterator for AHashSet<T, S>
impl<T, S> IntoIterator for AHashSet<T, S>
impl<'a, T, S> IntoIterator for &'a AHashSet<T, S>
impl<'a, T, S> IntoIterator for &'a AHashSet<T, S>
sourceimpl<'a, 'b, T> IntoIterator for &'b GetAll<'a, T> where
'b: 'a,
impl<'a, 'b, T> IntoIterator for &'b GetAll<'a, T> where
'b: 'a,
sourceimpl<'a, 'b, T> IntoIterator for &'b OccupiedEntry<'a, T> where
'b: 'a,
impl<'a, 'b, T> IntoIterator for &'b OccupiedEntry<'a, T> where
'b: 'a,
sourceimpl<'a, T> IntoIterator for &'a HeaderMap<T>
impl<'a, T> IntoIterator for &'a HeaderMap<T>
sourceimpl<T> IntoIterator for HeaderMap<T>
impl<T> IntoIterator for HeaderMap<T>
sourcefn into_iter(self) -> IntoIter<T>ⓘNotable traits for IntoIter<T>impl<T> Iterator for IntoIter<T> type Item = (Option<HeaderName>, T);
fn into_iter(self) -> IntoIter<T>ⓘNotable traits for IntoIter<T>impl<T> Iterator for IntoIter<T> type Item = (Option<HeaderName>, T);
Creates a consuming iterator, that is, one that moves keys and values out of the map in arbitrary order. The map cannot be used after calling this.
For each yielded item that has None
provided for the HeaderName
,
then the associated header name is the same as that of the previously
yielded item. The first yielded item will have HeaderName
set.
Examples
Basic usage.
let mut map = HeaderMap::new();
map.insert(header::CONTENT_LENGTH, "123".parse().unwrap());
map.insert(header::CONTENT_TYPE, "json".parse().unwrap());
let mut iter = map.into_iter();
assert_eq!(iter.next(), Some((Some(header::CONTENT_LENGTH), "123".parse().unwrap())));
assert_eq!(iter.next(), Some((Some(header::CONTENT_TYPE), "json".parse().unwrap())));
assert!(iter.next().is_none());
Multiple values per key.
let mut map = HeaderMap::new();
map.append(header::CONTENT_LENGTH, "123".parse().unwrap());
map.append(header::CONTENT_LENGTH, "456".parse().unwrap());
map.append(header::CONTENT_TYPE, "json".parse().unwrap());
map.append(header::CONTENT_TYPE, "html".parse().unwrap());
map.append(header::CONTENT_TYPE, "xml".parse().unwrap());
let mut iter = map.into_iter();
assert_eq!(iter.next(), Some((Some(header::CONTENT_LENGTH), "123".parse().unwrap())));
assert_eq!(iter.next(), Some((None, "456".parse().unwrap())));
assert_eq!(iter.next(), Some((Some(header::CONTENT_TYPE), "json".parse().unwrap())));
assert_eq!(iter.next(), Some((None, "html".parse().unwrap())));
assert_eq!(iter.next(), Some((None, "xml".parse().unwrap())));
assert!(iter.next().is_none());
type Item = (Option<HeaderName>, T)
type IntoIter = IntoIter<T>
sourceimpl<'a, T> IntoIterator for OccupiedEntry<'a, T>
impl<'a, T> IntoIterator for OccupiedEntry<'a, T>
type Item = &'a mut T
type IntoIter = ValueIterMut<'a, T>
fn into_iter(self) -> ValueIterMut<'a, T>ⓘNotable traits for ValueIterMut<'a, T>impl<'a, T> Iterator for ValueIterMut<'a, T> where
T: 'a, type Item = &'a mut T;
T: 'a, type Item = &'a mut T;
sourceimpl<'a, 'b, T> IntoIterator for &'b mut OccupiedEntry<'a, T> where
'b: 'a,
impl<'a, 'b, T> IntoIterator for &'b mut OccupiedEntry<'a, T> where
'b: 'a,
type Item = &'a mut T
type IntoIter = ValueIterMut<'a, T>
fn into_iter(self) -> ValueIterMut<'a, T>ⓘNotable traits for ValueIterMut<'a, T>impl<'a, T> Iterator for ValueIterMut<'a, T> where
T: 'a, type Item = &'a mut T;
T: 'a, type Item = &'a mut T;
sourceimpl<'a, T> IntoIterator for GetAll<'a, T>
impl<'a, T> IntoIterator for GetAll<'a, T>
sourceimpl<'a, T> IntoIterator for &'a mut HeaderMap<T>
impl<'a, T> IntoIterator for &'a mut HeaderMap<T>
sourceimpl<'a> IntoIterator for &'a FieldSet
impl<'a> IntoIterator for &'a FieldSet
sourceimpl IntoIterator for IndexVec
impl IntoIterator for IndexVec
sourcefn into_iter(self) -> IndexVecIntoIterⓘNotable traits for IndexVecIntoIterimpl Iterator for IndexVecIntoIter type Item = usize;
fn into_iter(self) -> IndexVecIntoIterⓘNotable traits for IndexVecIntoIterimpl Iterator for IndexVecIntoIter type Item = usize;
Convert into an iterator over the indices as a sequence of usize
values