pub trait IntoIterator {
type Item;
type IntoIter: Iterator<Item = Self::Item>;
// Required method
fn into_iter(self) -> Self::IntoIter;
}
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§
sourcefn into_iter(self) -> Self::IntoIter
fn into_iter(self) -> Self::IntoIter
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());
Implementors§
§impl IntoIterator for druid::piet::kurbo::BezPath
impl IntoIterator for druid::piet::kurbo::BezPath
§impl IntoIterator for FileAttributeMatcher
impl IntoIterator for FileAttributeMatcher
§impl<'a> IntoIterator for &'a druid::piet::kurbo::BezPath
impl<'a> IntoIterator for &'a druid::piet::kurbo::BezPath
Allow iteration over references to BezPath
.
Note: the semantics are slightly different from simply iterating over the
slice, as it returns PathEl
items, rather than references.
1.10.0 · source§impl<'a> IntoIterator for &'a UnixListener
impl<'a> IntoIterator for &'a UnixListener
source§impl<'a> IntoIterator for &'a Targets
impl<'a> IntoIterator for &'a Targets
§impl<'a> IntoIterator for &'a BezPath
impl<'a> IntoIterator for &'a BezPath
Allow iteration over references to BezPath
.
Note: the semantics are slightly different from simply iterating over the
slice, as it returns PathEl
items, rather than references.
§impl<'a> IntoIterator for &'a ListModel
impl<'a> IntoIterator for &'a ListModel
§impl<'a> IntoIterator for &'a ListStore
impl<'a> IntoIterator for &'a ListStore
type Item = <&'a ListModel as IntoIterator>::Item
type IntoIter = <&'a ListModel as IntoIterator>::IntoIter
§impl<'a> IntoIterator for &'a Utf8Sequence
impl<'a> IntoIterator for &'a Utf8Sequence
§impl<'a> IntoIterator for FeatureNames<'a>
impl<'a> IntoIterator for FeatureNames<'a>
§impl<'a> IntoIterator for SegmentMaps<'a>
impl<'a> IntoIterator for SegmentMaps<'a>
§impl<'a> IntoIterator for SvgDocumentsList<'a>
impl<'a> IntoIterator for SvgDocumentsList<'a>
source§impl<'a, A> IntoIterator for &'a OrdSet<A>where
A: 'a + Ord,
impl<'a, A> IntoIterator for &'a OrdSet<A>where A: 'a + Ord,
source§impl<'a, A> IntoIterator for &'a Vector<A>where
A: Clone,
impl<'a, A> IntoIterator for &'a Vector<A>where A: Clone,
§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,
source§impl<'a, A> IntoIterator for Focus<'a, A>where
A: Clone + 'a,
impl<'a, A> IntoIterator for Focus<'a, A>where A: Clone + 'a,
source§impl<'a, A> IntoIterator for FocusMut<'a, A>where
A: Clone + 'a,
impl<'a, A> IntoIterator for FocusMut<'a, A>where A: Clone + 'a,
§impl<'a, A, N> IntoIterator for &'a Chunk<A, N>where
N: ChunkLength<A>,
impl<'a, A, N> IntoIterator for &'a Chunk<A, N>where N: ChunkLength<A>,
§impl<'a, A, N> IntoIterator for &'a mut Chunk<A, N>where
N: ChunkLength<A>,
impl<'a, A, N> IntoIterator for &'a mut Chunk<A, N>where N: ChunkLength<A>,
source§impl<'a, A, S> IntoIterator for &'a druid::im::HashSet<A, S>where
A: Hash + Eq,
S: BuildHasher,
impl<'a, A, S> IntoIterator for &'a druid::im::HashSet<A, S>where A: Hash + Eq, S: BuildHasher,
§impl<'a, A, T> IntoIterator for &'a InlineArray<A, T>
impl<'a, A, T> IntoIterator for &'a InlineArray<A, T>
§impl<'a, A, T> IntoIterator for &'a mut InlineArray<A, T>
impl<'a, A, T> IntoIterator for &'a mut InlineArray<A, T>
§impl<'a, Fut> IntoIterator for &'a FuturesUnordered<Fut>where
Fut: Unpin,
impl<'a, Fut> IntoIterator for &'a FuturesUnordered<Fut>where Fut: Unpin,
§impl<'a, Fut> IntoIterator for &'a mut FuturesUnordered<Fut>where
Fut: Unpin,
impl<'a, Fut> IntoIterator for &'a mut FuturesUnordered<Fut>where Fut: Unpin,
type Item = &'a mut Fut
type IntoIter = IterMut<'a, Fut>
source§impl<'a, K, V> IntoIterator for &'a OrdMap<K, V>where
K: Ord,
impl<'a, K, V> IntoIterator for &'a OrdMap<K, V>where K: Ord,
source§impl<'a, K, V, S> IntoIterator for &'a druid::im::HashMap<K, V, S>where
K: Hash + Eq,
S: BuildHasher,
impl<'a, K, V, S> IntoIterator for &'a druid::im::HashMap<K, V, S>where K: Hash + Eq, S: BuildHasher,
source§impl<'a, K, V, S> IntoIterator for &'a std::collections::hash::map::HashMap<K, V, S>
impl<'a, K, V, S> IntoIterator for &'a std::collections::hash::map::HashMap<K, V, S>
source§impl<'a, K, V, S> IntoIterator for &'a mut std::collections::hash::map::HashMap<K, V, S>
impl<'a, K, V, S> IntoIterator for &'a mut std::collections::hash::map::HashMap<K, V, S>
§impl<'a, Size> IntoIterator for &'a Bitmap<Size>where
Size: Bits,
impl<'a, Size> IntoIterator for &'a Bitmap<Size>where Size: Bits,
§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<'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,
type Item = &'a mut St
type IntoIter = IterMut<'a, St>
1.4.0 · source§impl<'a, T> IntoIterator for &'a Option<T>
impl<'a, T> IntoIterator for &'a Option<T>
source§impl<'a, T> IntoIterator for &'a BinaryHeap<T>
impl<'a, T> IntoIterator for &'a BinaryHeap<T>
source§impl<'a, T> IntoIterator for &'a LinkedList<T>
impl<'a, T> IntoIterator for &'a LinkedList<T>
1.1.0 · source§impl<'a, T> IntoIterator for &'a Receiver<T>
impl<'a, T> IntoIterator for &'a Receiver<T>
§impl<'a, T> IntoIterator for &'a ThreadLocal<T>where
T: Send + Sync,
impl<'a, T> IntoIterator for &'a ThreadLocal<T>where T: Send + Sync,
1.4.0 · source§impl<'a, T> IntoIterator for &'a mut Option<T>
impl<'a, T> IntoIterator for &'a mut Option<T>
source§impl<'a, T> IntoIterator for &'a mut [T]
impl<'a, T> IntoIterator for &'a mut [T]
source§impl<'a, T> IntoIterator for &'a mut LinkedList<T>
impl<'a, T> IntoIterator for &'a mut LinkedList<T>
§impl<'a, T> IntoIterator for &'a mut CachedThreadLocal<T>where
T: Send + 'a,
impl<'a, T> IntoIterator for &'a mut CachedThreadLocal<T>where T: Send + 'a,
§impl<'a, T> IntoIterator for &'a mut Slab<T>
impl<'a, T> IntoIterator for &'a mut Slab<T>
§impl<'a, T> IntoIterator for &'a mut ThreadLocal<T>where
T: Send,
impl<'a, T> IntoIterator for &'a mut ThreadLocal<T>where T: Send,
§impl<'a, T> IntoIterator for LazyArray16<'a, T>where
T: FromData,
impl<'a, T> IntoIterator for LazyArray16<'a, T>where T: FromData,
§impl<'a, T> IntoIterator for LazyArray32<'a, T>where
T: FromData,
impl<'a, T> IntoIterator for LazyArray32<'a, T>where T: FromData,
§impl<'a, T> IntoIterator for RecordList<'a, T>where
T: RecordListItem<'a>,
impl<'a, T> IntoIterator for RecordList<'a, T>where T: RecordListItem<'a>,
source§impl<'a, T, A> IntoIterator for &'a VecDeque<T, A>where
A: Allocator,
impl<'a, T, A> IntoIterator for &'a VecDeque<T, A>where A: Allocator,
source§impl<'a, T, A> IntoIterator for &'a Vec<T, A>where
A: Allocator,
impl<'a, T, A> IntoIterator for &'a Vec<T, A>where A: Allocator,
source§impl<'a, T, A> IntoIterator for &'a mut VecDeque<T, A>where
A: Allocator,
impl<'a, T, A> IntoIterator for &'a mut VecDeque<T, A>where A: Allocator,
source§impl<'a, T, A> IntoIterator for &'a mut Vec<T, A>where
A: Allocator,
impl<'a, T, A> IntoIterator for &'a mut Vec<T, A>where A: Allocator,
1.4.0 · source§impl<'a, T, E> IntoIterator for &'a Result<T, E>
impl<'a, T, E> IntoIterator for &'a Result<T, E>
1.4.0 · source§impl<'a, T, E> IntoIterator for &'a mut Result<T, E>
impl<'a, T, E> IntoIterator for &'a mut Result<T, E>
source§impl<'a, T, S> IntoIterator for &'a std::collections::hash::set::HashSet<T, S>
impl<'a, T, S> IntoIterator for &'a std::collections::hash::set::HashSet<T, S>
source§impl<'a, T, const CAP: usize> IntoIterator for &'a ArrayVec<T, CAP>where
T: 'a,
impl<'a, T, const CAP: usize> IntoIterator for &'a ArrayVec<T, CAP>where T: 'a,
Iterate the ArrayVec
with references to each element.
use arrayvec::ArrayVec;
let array = ArrayVec::from([1, 2, 3]);
for elt in &array {
// ...
}
source§impl<'a, T, const CAP: usize> IntoIterator for &'a mut ArrayVec<T, CAP>where
T: 'a,
impl<'a, T, const CAP: usize> IntoIterator for &'a mut ArrayVec<T, CAP>where T: 'a,
Iterate the ArrayVec
with mutable references to each element.
use arrayvec::ArrayVec;
let mut array = ArrayVec::from([1, 2, 3]);
for elt in &mut array {
// ...
}
source§impl<'a, T, const N: usize> IntoIterator for &'a [T; N]
impl<'a, T, const N: usize> IntoIterator for &'a [T; N]
source§impl<'a, T, const N: usize> IntoIterator for &'a mut [T; N]
impl<'a, T, const N: usize> IntoIterator for &'a mut [T; N]
source§impl<'args> IntoIterator for FluentArgs<'args>
impl<'args> IntoIterator for FluentArgs<'args>
type Item = (Cow<'args, str>, FluentValue<'args>)
type IntoIter = IntoIter<<FluentArgs<'args> as IntoIterator>::Item, Global>
§impl<'item> IntoIterator for GlyphItemIter<'item>
impl<'item> IntoIterator for GlyphItemIter<'item>
§impl<'list> IntoIterator for AttrIterator<'list>
impl<'list> IntoIterator for AttrIterator<'list>
§impl<'text> IntoIterator for ScriptIter<'text>
impl<'text> IntoIterator for ScriptIter<'text>
source§impl<A> IntoIterator for OrdSet<A>where
A: Ord + Clone,
impl<A> IntoIterator for OrdSet<A>where A: Ord + Clone,
type Item = A
type IntoIter = ConsumingIter<A>
source§impl<A> IntoIterator for Vector<A>where
A: Clone,
impl<A> IntoIterator for Vector<A>where A: Clone,
type Item = A
type IntoIter = ConsumingIter<A>
§impl<A> IntoIterator for SmallVec<A>where
A: Array,
impl<A> IntoIterator for SmallVec<A>where A: Array,
§impl<A, N> IntoIterator for Chunk<A, N>where
N: ChunkLength<A>,
impl<A, N> IntoIterator for Chunk<A, N>where N: ChunkLength<A>,
§impl<A, N> IntoIterator for SparseChunk<A, N>where
N: Bits + ChunkLength<A>,
impl<A, N> IntoIterator for SparseChunk<A, N>where N: Bits + ChunkLength<A>,
source§impl<A, S> IntoIterator for druid::im::HashSet<A, S>where
A: Hash + Eq + Clone,
S: BuildHasher,
impl<A, S> IntoIterator for druid::im::HashSet<A, S>where A: Hash + Eq + Clone, S: BuildHasher,
type Item = A
type IntoIter = ConsumingIter<<HashSet<A, S> as IntoIterator>::Item>
§impl<Fut> IntoIterator for FuturesUnordered<Fut>where
Fut: Unpin,
impl<Fut> IntoIterator for FuturesUnordered<Fut>where Fut: Unpin,
const: unstable · source§impl<I> IntoIterator for Iwhere
I: Iterator,
impl<I> IntoIterator for Iwhere I: Iterator,
source§impl<K, V, S> IntoIterator for druid::im::HashMap<K, V, S>where
K: Hash + Eq + Clone,
V: Clone,
S: BuildHasher,
impl<K, V, S> IntoIterator for druid::im::HashMap<K, V, S>where K: Hash + Eq + Clone, V: Clone, S: BuildHasher,
source§impl<K, V, S> IntoIterator for std::collections::hash::map::HashMap<K, V, S>
impl<K, V, S> IntoIterator for std::collections::hash::map::HashMap<K, V, S>
§impl<St> IntoIterator for SelectAll<St>where
St: Stream + Unpin,
impl<St> IntoIterator for SelectAll<St>where St: Stream + Unpin,
source§impl<T> IntoIterator for BinaryHeap<T>
impl<T> IntoIterator for BinaryHeap<T>
source§impl<T> IntoIterator for LinkedList<T>
impl<T> IntoIterator for LinkedList<T>
§impl<T> IntoIterator for CachedThreadLocal<T>where
T: Send,
impl<T> IntoIterator for CachedThreadLocal<T>where T: Send,
source§impl<T, A> IntoIterator for VecDeque<T, A>where
A: Allocator,
impl<T, A> IntoIterator for VecDeque<T, A>where A: Allocator,
source§impl<T, A> IntoIterator for Vec<T, A>where
A: Allocator,
impl<T, A> IntoIterator for Vec<T, A>where A: Allocator,
source§impl<T, S> IntoIterator for std::collections::hash::set::HashSet<T, S>
impl<T, S> IntoIterator for std::collections::hash::set::HashSet<T, S>
source§impl<T, const CAP: usize> IntoIterator for ArrayVec<T, CAP>
impl<T, const CAP: usize> IntoIterator for ArrayVec<T, CAP>
Iterate the ArrayVec
with each element by value.
The vector is consumed by this operation.
use arrayvec::ArrayVec;
for elt in ArrayVec::from([1, 2, 3]) {
// ...
}