Trait libc::prelude::iter::IntoIterator

1.0.0 · source ·
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§

1.0.0 · source

type Item

The type of the elements being iterated over.

1.0.0 · source

type IntoIter: Iterator<Item = Self::Item>

Which kind of iterator are we turning this into?

Required Methods§

1.0.0 · source

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value.

See the module-level documentation for more.

§Examples
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§

1.4.0 · source§

impl<'a, T> IntoIterator for &'a Option<T>

1.0.0 · source§

impl<'a, T> IntoIterator for &'a [T]

1.4.0 · source§

impl<'a, T> IntoIterator for &'a mut Option<T>

1.0.0 · source§

impl<'a, T> IntoIterator for &'a mut [T]

1.4.0 · source§

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>

1.0.0 · source§

impl<'a, T, const N: usize> IntoIterator for &'a [T; N]

1.0.0 · source§

impl<'a, T, const N: usize> IntoIterator for &'a mut [T; N]

source§

impl<A> IntoIterator for Range<A>
where A: Step,

source§

impl<A> IntoIterator for RangeFrom<A>
where A: Step,

source§

impl<A> IntoIterator for RangeInclusive<A>
where A: Step,

1.0.0 (const: unstable) · source§

impl<I> IntoIterator for I
where I: Iterator,

1.0.0 · source§

impl<T> IntoIterator for Option<T>

1.0.0 · source§

impl<T, E> IntoIterator for Result<T, E>

1.53.0 · source§

impl<T, const N: usize> IntoIterator for [T; N]

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, T, S> IntoIterator for &'a AHashSet<T, S>

impl<K, V, S> IntoIterator for AHashMap<K, V, S>

impl<T, S> IntoIterator for AHashSet<T, S>

impl<'a, T: 'a, const CAP: usize> IntoIterator for &'a ArrayVec<T, CAP>

impl<'a, T: 'a, const CAP: usize> IntoIterator for &'a mut ArrayVec<T, CAP>

impl<T, const CAP: usize> IntoIterator for ArrayVec<T, CAP>

impl<'a, B: BitBlock> IntoIterator for &'a BitSet<B>

impl<'a, B: BitBlock> IntoIterator for &'a BitVec<B>

impl<B: BitBlock> IntoIterator for BitVec<B>

impl<'a> IntoIterator for &'a Bytes

impl<'a> IntoIterator for &'a BytesMut

impl<T, U> IntoIterator for Chain<T, U>
where T: Buf, U: Buf,

impl<'a, T> IntoIterator for &'a Receiver<T>

impl<T> IntoIterator for Receiver<T>

impl<T> IntoIterator for Fields<T>

impl<'l> IntoIterator for &'l AtlasAllocator

impl<'a, Fut: Unpin> IntoIterator for &'a FuturesUnordered<Fut>

impl<'a, Fut: Unpin> IntoIterator for &'a mut FuturesUnordered<Fut>

impl<'a, St: Stream + Unpin> IntoIterator for &'a SelectAll<St>

impl<'a, St: Stream + Unpin> IntoIterator for &'a mut SelectAll<St>

impl<Fut: Unpin> IntoIterator for FuturesUnordered<Fut>

impl<St: Stream + Unpin> IntoIterator for SelectAll<St>

impl<'a, T: 'a, N> IntoIterator for &'a GenericArray<T, N>
where N: ArrayLength<T>,

impl<'a, T: 'a, N> IntoIterator for &'a mut GenericArray<T, N>
where N: ArrayLength<T>,

impl<T, N> IntoIterator for GenericArray<T, N>
where N: ArrayLength<T>,

impl<R: Read> IntoIterator for Decoder<R>

impl<'a> IntoIterator for &'a StrV

impl<'a, T: TransparentPtrType> IntoIterator for &'a List<T>

impl<'a, T: TransparentPtrType> IntoIterator for &'a PtrSlice<T>

impl<'a, T: TransparentPtrType> IntoIterator for &'a SList<T>

impl<'a, T: TransparentPtrType> IntoIterator for &'a mut List<T>

impl<'a, T: TransparentPtrType> IntoIterator for &'a mut PtrSlice<T>

impl<'a, T: TransparentPtrType> IntoIterator for &'a mut SList<T>

impl<'a, T: TransparentType> IntoIterator for &'a Slice<T>

impl<'a, T: TransparentType> IntoIterator for &'a mut Slice<T>

impl<'a> IntoIterator for &'a BufferRef

impl<'a> IntoIterator for &'a BufferListRef

impl<'a> IntoIterator for &'a CapsRef

impl<'a> IntoIterator for &'a CapsFeaturesRef

impl<'a> IntoIterator for &'a StructureRef

impl<'a> IntoIterator for &'a mut CapsRef

impl<T> IntoIterator for Iterator<T>
where for<'a> T: FromValue<'a> + 'static,

impl<'a, K, V, S, A: Allocator> IntoIterator for &'a HashMap<K, V, S, A>

impl<'a, K, V, S, A: Allocator> IntoIterator for &'a mut HashMap<K, V, S, A>

impl<'a, T, A> IntoIterator for &'a HashTable<T, A>
where A: Allocator,

impl<'a, T, A> IntoIterator for &'a mut HashTable<T, A>
where A: Allocator,

impl<'a, T, S, A: Allocator> IntoIterator for &'a HashSet<T, S, A>

impl<K, V, S, A: Allocator> IntoIterator for HashMap<K, V, S, A>

impl<T, A> IntoIterator for HashTable<T, A>
where A: Allocator,

impl<T, A: Allocator> IntoIterator for RawTable<T, A>

impl<T, S, A: Allocator> IntoIterator for HashSet<T, S, A>

impl<'a, 'b: 'a, T> IntoIterator for &'b GetAll<'a, T>

impl<'a, 'b: 'a, T> IntoIterator for &'b OccupiedEntry<'a, T>

impl<'a, 'b: 'a, T> IntoIterator for &'b mut OccupiedEntry<'a, T>

impl<'a, T> IntoIterator for &'a HeaderMap<T>

impl<'a, T> IntoIterator for &'a mut HeaderMap<T>

impl<'a, T> IntoIterator for GetAll<'a, T>

impl<'a, T> IntoIterator for OccupiedEntry<'a, T>

impl<T> IntoIterator for HeaderMap<T>

impl<R: Read> IntoIterator for HdrDecoder<R>

impl<'a, K, V> IntoIterator for &'a Slice<K, V>

impl<'a, K, V> IntoIterator for &'a mut Slice<K, V>

impl<'a, K, V, S> IntoIterator for &'a IndexMap<K, V, S>

impl<'a, K, V, S> IntoIterator for &'a mut IndexMap<K, V, S>

impl<'a, T> IntoIterator for &'a Slice<T>

impl<'a, T, S> IntoIterator for &'a IndexSet<T, S>

impl<K, V> IntoIterator for Box<Slice<K, V>>

impl<K, V, S> IntoIterator for IndexMap<K, V, S>

impl<T> IntoIterator for Box<Slice<T>>

impl<T, S> IntoIterator for IndexSet<T, S>

impl<'inp, 'out, T> IntoIterator for InOutBuf<'inp, 'out, T>

impl<'a, I> IntoIterator for &'a IntoChunks<I>
where I: Iterator, I::Item: 'a,

impl<'a, I> IntoIterator for &'a RcIter<I>
where I: Iterator,

impl<'a, K, I, F> IntoIterator for &'a ChunkBy<K, I, F>
where I: Iterator, I::Item: 'a, F: FnMut(&I::Item) -> K, K: PartialEq,

impl<K, V, S> IntoIterator for LiteMap<K, V, S>
where S: StoreIntoIterator<K, V>,

impl<'a> IntoIterator for &'a MimeGuess

impl<'a> IntoIterator for &'a Events

impl<'a> IntoIterator for &'a Block

impl<'a> IntoIterator for &'a SigSet

impl<'a, K, V, S> IntoIterator for &'a OrderMap<K, V, S>
where K: Hash + Eq, S: BuildHasher,

impl<'a, K, V, S> IntoIterator for &'a mut OrderMap<K, V, S>
where K: Hash + Eq, S: BuildHasher,

impl<'a, T, S> IntoIterator for &'a OrderSet<T, S>
where T: Hash + Eq, S: BuildHasher,

impl<K, V, S> IntoIterator for OrderMap<K, V, S>
where K: Hash + Eq, S: BuildHasher,

impl<T, S> IntoIterator for OrderSet<T, S>
where T: Hash + Eq, S: BuildHasher,

impl<'a, K, V> IntoIterator for &'a Map<K, V>

impl<'a, K, V> IntoIterator for &'a OrderedMap<K, V>

impl<'a, T> IntoIterator for &'a OrderedSet<T>

impl<'a, T> IntoIterator for &'a Set<T>

impl<T> IntoIterator for RcVecBuilder<T>

impl<T> IntoIterator for IterProducer<T>
where Range<T>: Iterator,

impl<'a> IntoIterator for &'a SetMatches

impl<'a> IntoIterator for &'a SetMatches

impl<'a> IntoIterator for &'a Utf8Sequence

impl IntoIterator for Map

impl<'a> IntoIterator for &'a EventVec

impl<'a> IntoIterator for &'a ByteBuf

impl<'a> IntoIterator for &'a Bytes

impl<'a> IntoIterator for &'a mut ByteBuf

impl<'a> IntoIterator for &'a mut Bytes

impl<'a, const N: usize> IntoIterator for &'a ByteArray<N>

impl<'a, const N: usize> IntoIterator for &'a mut ByteArray<N>

impl<const N: usize> IntoIterator for ByteArray<N>

impl<'a> IntoIterator for &'a Map<String, Value>

impl<'a> IntoIterator for &'a mut Map<String, Value>

impl<'a, T> IntoIterator for &'a Slab<T>

impl<'a, T> IntoIterator for &'a mut Slab<T>

impl<T> IntoIterator for Slab<T>

impl<'a> IntoIterator for &'a SmallBitVec

impl<'a, A: Array> IntoIterator for &'a SmallVec<A>

impl<'a, A: Array> IntoIterator for &'a mut SmallVec<A>

impl<A: Array> IntoIterator for SmallVec<A>

impl<'a, 'b> IntoIterator for &'a StringWrapper<'b>

impl<'a, K, V> IntoIterator for &'a Map<K, V>

impl<T> IntoIterator for OwnedSlice<T>

impl<'a> IntoIterator for &'a Fields

impl<'a> IntoIterator for &'a Error

impl<'a> IntoIterator for &'a mut Fields

impl<'a, T, P> IntoIterator for &'a Punctuated<T, P>

impl<'a, T, P> IntoIterator for &'a mut Punctuated<T, P>

impl<T, P> IntoIterator for Punctuated<T, P>

impl<'a, T> IntoIterator for &'a ThinVec<T>

impl<'a, T> IntoIterator for &'a mut ThinVec<T>

impl<T> IntoIterator for ThinVec<T>

impl<'s> IntoIterator for &'s Array

impl<'s> IntoIterator for &'s ArrayOfTables

impl<'s> IntoIterator for &'s InlineTable

impl<'s> IntoIterator for &'s Table

impl<'a> IntoIterator for &'a FieldSet

impl<'a> IntoIterator for SegmentMaps<'a>

impl<'a> IntoIterator for Index<'a>

impl<'a> IntoIterator for Subtables<'a>

impl<'a> IntoIterator for FeatureNames<'a>

impl<'a> IntoIterator for Subtables<'a>

impl<'a> IntoIterator for Subtables<'a>

impl<'a> IntoIterator for Chains<'a>

impl<'a> IntoIterator for Subtables<'a>

impl<'a> IntoIterator for Names<'a>

impl<'a> IntoIterator for Strikes<'a>

impl<'a> IntoIterator for Tracks<'a>

impl<'a, T: RecordListItem<'a>> IntoIterator for RecordList<'a, T>

impl<'a, T: FromData> IntoIterator for LazyArray16<'a, T>

impl<'a, T: FromData> IntoIterator for LazyArray32<'a, T>

impl<'a, T: FromSlice<'a>> IntoIterator for LazyOffsetArray16<'a, T>

impl<'a, T> IntoIterator for &'a VecMap<T>

impl<'a, T> IntoIterator for &'a mut VecMap<T>

impl<T> IntoIterator for VecMap<T>

impl<'a, T> IntoIterator for ItemRange<'a, T>
where T: Copy + Default + Peek,

impl<'a, T> IntoIterator for &'a StatelessTracker<T>

impl<T> IntoIterator for WeakVec<T>

impl<'a> IntoIterator for &'a Keymap