std::iter::ExactSizeIterator - Rust (original) (raw)
Trait std::iter::ExactSizeIterator1.0.0 [−] [src]
pub trait ExactSizeIterator: Iterator { fn len(&self) -> usize { ... } fn is_empty(&self) -> bool { ... } }
An iterator that knows its exact length.
Many Iterators don't know how many times they will iterate, but some do. If an iterator knows how many times it can iterate, providing access to that information can be useful. For example, if you want to iterate backwards, a good start is to know where the end is.
When implementing an ExactSizeIterator
, You must also implementIterator. When doing so, the implementation of size_hint _must_return the exact size of the iterator.
The len method has a default implementation, so you usually shouldn't implement it. However, you may be able to provide a more performant implementation than the default, so overriding it in this case makes sense.
Basic usage:
let five = 0..5;
assert_eq!(5, five.len());Run
In the module level docs, we implemented an Iterator,Counter
. Let's implement ExactSizeIterator
for it as well:
impl ExactSizeIterator for Counter {
fn len(&self) -> usize {
5 - self.count
}
}
let counter = Counter::new();
assert_eq!(5, counter.len());Run
fn [len](#method.len)(&self) -> [usize](../primitive.usize.html)
Returns the exact number of times the iterator will iterate.
This method has a default implementation, so you usually should not implement it directly. However, if you can provide a more efficient implementation, you can do so. See the trait-level docs for an example.
This function has the same safety guarantees as the size_hintfunction.
Basic usage:
let five = 0..5;
assert_eq!(5, five.len());Run
fn [is_empty](#method.is%5Fempty)(&self) -> [bool](../primitive.bool.html)
🔬 This is a nightly-only experimental API. (exact_size_is_empty
#35428)
Returns whether the iterator is empty.
This method has a default implementation using self.len()
, so you don't need to implement it yourself.
Basic usage:
#![feature(exact_size_is_empty)]
let mut one_element = 0..1; assert!(!one_element.is_empty());
assert_eq!(one_element.next(), Some(0)); assert!(one_element.is_empty());
assert_eq!(one_element.next(), None);Run
impl<'a, T> ExactSizeIterator for [ExactChunksMut](../../std/slice/struct.ExactChunksMut.html "struct std::slice::ExactChunksMut")<'a, T>
impl ExactSizeIterator for [Range](../../std/ops/struct.Range.html "struct std::ops::Range")<[u8](../primitive.u8.html)>
impl<T> ExactSizeIterator for [Empty](../../std/iter/struct.Empty.html "struct std::iter::Empty")<T>
impl<T> ExactSizeIterator for [Once](../../std/iter/struct.Once.html "struct std::iter::Once")<T>
impl<I> ExactSizeIterator for [Rev](../../std/iter/struct.Rev.html "struct std::iter::Rev")<I> where I: [ExactSizeIterator](../../std/iter/trait.ExactSizeIterator.html "trait std::iter::ExactSizeIterator") + [DoubleEndedIterator](../../std/iter/trait.DoubleEndedIterator.html "trait std::iter::DoubleEndedIterator"),
impl ExactSizeIterator for [Range](../../std/ops/struct.Range.html "struct std::ops::Range")<[usize](../primitive.usize.html)>
impl<I> ExactSizeIterator for [Peekable](../../std/iter/struct.Peekable.html "struct std::iter::Peekable")<I> where I: [ExactSizeIterator](../../std/iter/trait.ExactSizeIterator.html "trait std::iter::ExactSizeIterator"),
impl<B, I, F> ExactSizeIterator for [Map](../../std/iter/struct.Map.html "struct std::iter::Map")<I, F> where F: [FnMut](../../std/ops/trait.FnMut.html "trait std::ops::FnMut")(<I as [Iterator](../../std/iter/trait.Iterator.html "trait std::iter::Iterator")>::[Item](../../std/iter/trait.Iterator.html#associatedtype.Item "type std::iter::Iterator::Item")) -> B, I: [ExactSizeIterator](../../std/iter/trait.ExactSizeIterator.html "trait std::iter::ExactSizeIterator"),
impl<'a, A> ExactSizeIterator for std::option::[IterMut](../../std/option/struct.IterMut.html "struct std::option::IterMut")<'a, A>
impl ExactSizeIterator for [RangeInclusive](../../std/ops/struct.RangeInclusive.html "struct std::ops::RangeInclusive")<[i16](../primitive.i16.html)>
impl ExactSizeIterator for [Range](../../std/ops/struct.Range.html "struct std::ops::Range")<[i8](../primitive.i8.html)>
impl ExactSizeIterator for [Range](../../std/ops/struct.Range.html "struct std::ops::Range")<[i16](../primitive.i16.html)>
impl<I> ExactSizeIterator for [Enumerate](../../std/iter/struct.Enumerate.html "struct std::iter::Enumerate")<I> where I: [ExactSizeIterator](../../std/iter/trait.ExactSizeIterator.html "trait std::iter::ExactSizeIterator"),
impl<'a, T> ExactSizeIterator for std::slice::[Iter](../../std/slice/struct.Iter.html "struct std::slice::Iter")<'a, T>
impl ExactSizeIterator for std::char::[EscapeDefault](../../std/char/struct.EscapeDefault.html "struct std::char::EscapeDefault")
impl<'a, I> ExactSizeIterator for [&'a mut ](../primitive.reference.html)I where I: [ExactSizeIterator](../../std/iter/trait.ExactSizeIterator.html "trait std::iter::ExactSizeIterator") + ?[Sized](../../std/marker/trait.Sized.html "trait std:📑:Sized"),
impl ExactSizeIterator for [Range](../../std/ops/struct.Range.html "struct std::ops::Range")<[u16](../primitive.u16.html)>
impl ExactSizeIterator for [EscapeDebug](../../std/char/struct.EscapeDebug.html "struct std::char::EscapeDebug")
impl<'a, T> ExactSizeIterator for std::result::[Iter](../../std/result/struct.Iter.html "struct std::result::Iter")<'a, T>
impl<'a, I, T> ExactSizeIterator for [Cloned](../../std/iter/struct.Cloned.html "struct std::iter::Cloned")<I> where I: [ExactSizeIterator](../../std/iter/trait.ExactSizeIterator.html "trait std::iter::ExactSizeIterator")<Item = [&'a ](../primitive.reference.html)T>, T: 'a + [Clone](../../std/clone/trait.Clone.html "trait std::clone::Clone"),
impl ExactSizeIterator for [RangeInclusive](../../std/ops/struct.RangeInclusive.html "struct std::ops::RangeInclusive")<[i8](../primitive.i8.html)>
impl ExactSizeIterator for [Range](../../std/ops/struct.Range.html "struct std::ops::Range")<[i32](../primitive.i32.html)>
impl ExactSizeIterator for [Range](../../std/ops/struct.Range.html "struct std::ops::Range")<[u32](../primitive.u32.html)>
impl<'a> ExactSizeIterator for [Bytes](../../std/str/struct.Bytes.html "struct std::str::Bytes")<'a>
impl<I> ExactSizeIterator for [Take](../../std/iter/struct.Take.html "struct std::iter::Take")<I> where I: [ExactSizeIterator](../../std/iter/trait.ExactSizeIterator.html "trait std::iter::ExactSizeIterator"),
impl<I> ExactSizeIterator for [StepBy](../../std/iter/struct.StepBy.html "struct std::iter::StepBy")<I> where I: [ExactSizeIterator](../../std/iter/trait.ExactSizeIterator.html "trait std::iter::ExactSizeIterator"),
impl<'a, T> ExactSizeIterator for [Windows](../../std/slice/struct.Windows.html "struct std::slice::Windows")<'a, T>
impl<T> ExactSizeIterator for std::result::[IntoIter](../../std/result/struct.IntoIter.html "struct std::result::IntoIter")<T>
impl ExactSizeIterator for [EscapeUnicode](../../std/char/struct.EscapeUnicode.html "struct std::char::EscapeUnicode")
impl ExactSizeIterator for [RangeInclusive](../../std/ops/struct.RangeInclusive.html "struct std::ops::RangeInclusive")<[u8](../primitive.u8.html)>
impl ExactSizeIterator for [Range](../../std/ops/struct.Range.html "struct std::ops::Range")<[isize](../primitive.isize.html)>
impl<I, F> ExactSizeIterator for [Inspect](../../std/iter/struct.Inspect.html "struct std::iter::Inspect")<I, F> where F: [FnMut](../../std/ops/trait.FnMut.html "trait std::ops::FnMut")(&<I as [Iterator](../../std/iter/trait.Iterator.html "trait std::iter::Iterator")>::[Item](../../std/iter/trait.Iterator.html#associatedtype.Item "type std::iter::Iterator::Item")), I: [ExactSizeIterator](../../std/iter/trait.ExactSizeIterator.html "trait std::iter::ExactSizeIterator"),
impl<'a, A> ExactSizeIterator for std::option::[Iter](../../std/option/struct.Iter.html "struct std::option::Iter")<'a, A>
impl<'a, T> ExactSizeIterator for [Chunks](../../std/slice/struct.Chunks.html "struct std::slice::Chunks")<'a, T>
impl<'a, T> ExactSizeIterator for std::result::[IterMut](../../std/result/struct.IterMut.html "struct std::result::IterMut")<'a, T>
impl<A> ExactSizeIterator for std::option::[IntoIter](../../std/option/struct.IntoIter.html "struct std::option::IntoIter")<A>
impl<'a, T> ExactSizeIterator for [ExactChunks](../../std/slice/struct.ExactChunks.html "struct std::slice::ExactChunks")<'a, T>
impl<A, B> ExactSizeIterator for [Zip](../../std/iter/struct.Zip.html "struct std::iter::Zip")<A, B> where A: [ExactSizeIterator](../../std/iter/trait.ExactSizeIterator.html "trait std::iter::ExactSizeIterator"), B: [ExactSizeIterator](../../std/iter/trait.ExactSizeIterator.html "trait std::iter::ExactSizeIterator"),
impl<I> ExactSizeIterator for [Fuse](../../std/iter/struct.Fuse.html "struct std::iter::Fuse")<I> where I: [ExactSizeIterator](../../std/iter/trait.ExactSizeIterator.html "trait std::iter::ExactSizeIterator"),
impl ExactSizeIterator for [RangeInclusive](../../std/ops/struct.RangeInclusive.html "struct std::ops::RangeInclusive")<[u16](../primitive.u16.html)>
impl<I> ExactSizeIterator for [Skip](../../std/iter/struct.Skip.html "struct std::iter::Skip")<I> where I: [ExactSizeIterator](../../std/iter/trait.ExactSizeIterator.html "trait std::iter::ExactSizeIterator"),
impl<'a, T> ExactSizeIterator for [ChunksMut](../../std/slice/struct.ChunksMut.html "struct std::slice::ChunksMut")<'a, T>
impl<'a, T> ExactSizeIterator for std::slice::[IterMut](../../std/slice/struct.IterMut.html "struct std::slice::IterMut")<'a, T>
impl<'a, T> ExactSizeIterator for std::collections::vec_deque::[Iter](../../std/collections/vec%5Fdeque/struct.Iter.html "struct std::collections::vec_deque::Iter")<'a, T>
impl<'a, T> ExactSizeIterator for std::collections::binary_heap::[Drain](../../std/collections/binary%5Fheap/struct.Drain.html "struct std::collections::binary_heap::Drain")<'a, T> where T: 'a,
impl<T> ExactSizeIterator for std::collections::linked_list::[IntoIter](../../std/collections/linked%5Flist/struct.IntoIter.html "struct std::collections::linked_list::IntoIter")<T>
impl<'a, T> ExactSizeIterator for std::collections::binary_heap::[Iter](../../std/collections/binary%5Fheap/struct.Iter.html "struct std::collections::binary_heap::Iter")<'a, T>
impl<I> ExactSizeIterator for [Box](../../std/boxed/struct.Box.html "struct std::boxed::Box")<I> where I: [ExactSizeIterator](../../std/iter/trait.ExactSizeIterator.html "trait std::iter::ExactSizeIterator") + ?[Sized](../../std/marker/trait.Sized.html "trait std:📑:Sized"),
impl<T> ExactSizeIterator for std::collections::binary_heap::[IntoIter](../../std/collections/binary%5Fheap/struct.IntoIter.html "struct std::collections::binary_heap::IntoIter")<T>
impl<T> ExactSizeIterator for std::collections::vec_deque::[IntoIter](../../std/collections/vec%5Fdeque/struct.IntoIter.html "struct std::collections::vec_deque::IntoIter")<T>
impl<'a, T> ExactSizeIterator for std::collections::vec_deque::[Drain](../../std/collections/vec%5Fdeque/struct.Drain.html "struct std::collections::vec_deque::Drain")<'a, T> where T: 'a,
impl<'a, T> ExactSizeIterator for std::collections::linked_list::[IterMut](../../std/collections/linked%5Flist/struct.IterMut.html "struct std::collections::linked_list::IterMut")<'a, T>
impl<'a, K, V> ExactSizeIterator for std::collections::btree_map::[Keys](../../std/collections/btree%5Fmap/struct.Keys.html "struct std::collections::btree_map::Keys")<'a, K, V>
impl<'a, K, V> ExactSizeIterator for std::collections::btree_map::[IterMut](../../std/collections/btree%5Fmap/struct.IterMut.html "struct std::collections::btree_map::IterMut")<'a, K, V> where K: 'a, V: 'a,
impl<'a, K, V> ExactSizeIterator for std::collections::btree_map::[ValuesMut](../../std/collections/btree%5Fmap/struct.ValuesMut.html "struct std::collections::btree_map::ValuesMut")<'a, K, V>
impl<T> ExactSizeIterator for std::collections::btree_set::[IntoIter](../../std/collections/btree%5Fset/struct.IntoIter.html "struct std::collections::btree_set::IntoIter")<T>
impl<'a, I> ExactSizeIterator for [Splice](../../std/vec/struct.Splice.html "struct std::vec::Splice")<'a, I> where I: [Iterator](../../std/iter/trait.Iterator.html "trait std::iter::Iterator"),
impl<'a, T> ExactSizeIterator for std::collections::linked_list::[Iter](../../std/collections/linked%5Flist/struct.Iter.html "struct std::collections::linked_list::Iter")<'a, T>
impl<'a, K, V> ExactSizeIterator for std::collections::btree_map::[Values](../../std/collections/btree%5Fmap/struct.Values.html "struct std::collections::btree_map::Values")<'a, K, V>
impl<'a, T> ExactSizeIterator for std::collections::btree_set::[Iter](../../std/collections/btree%5Fset/struct.Iter.html "struct std::collections::btree_set::Iter")<'a, T>
impl<T> ExactSizeIterator for std::vec::[IntoIter](../../std/vec/struct.IntoIter.html "struct std::vec::IntoIter")<T>
impl<'a, T> ExactSizeIterator for std::collections::vec_deque::[IterMut](../../std/collections/vec%5Fdeque/struct.IterMut.html "struct std::collections::vec_deque::IterMut")<'a, T>
impl<K, V> ExactSizeIterator for std::collections::btree_map::[IntoIter](../../std/collections/btree%5Fmap/struct.IntoIter.html "struct std::collections::btree_map::IntoIter")<K, V>
impl<'a, T> ExactSizeIterator for std::vec::[Drain](../../std/vec/struct.Drain.html "struct std::vec::Drain")<'a, T>
impl<'a, K, V> ExactSizeIterator for std::collections::btree_map::[Iter](../../std/collections/btree%5Fmap/struct.Iter.html "struct std::collections::btree_map::Iter")<'a, K, V> where K: 'a, V: 'a,
impl ExactSizeIterator for std::ascii::[EscapeDefault](../../std/ascii/struct.EscapeDefault.html "struct std::ascii::EscapeDefault")
impl<'a, K, V> ExactSizeIterator for std::collections::hash_map::[Iter](../../std/collections/hash%5Fmap/struct.Iter.html "struct std::collections::hash_map::Iter")<'a, K, V>
impl<'a, K, V> ExactSizeIterator for std::collections::hash_map::[IterMut](../../std/collections/hash%5Fmap/struct.IterMut.html "struct std::collections::hash_map::IterMut")<'a, K, V>
impl<K, V> ExactSizeIterator for std::collections::hash_map::[IntoIter](../../std/collections/hash%5Fmap/struct.IntoIter.html "struct std::collections::hash_map::IntoIter")<K, V>
impl<'a, K, V> ExactSizeIterator for std::collections::hash_map::[Keys](../../std/collections/hash%5Fmap/struct.Keys.html "struct std::collections::hash_map::Keys")<'a, K, V>
impl<'a, K, V> ExactSizeIterator for std::collections::hash_map::[Values](../../std/collections/hash%5Fmap/struct.Values.html "struct std::collections::hash_map::Values")<'a, K, V>
impl<'a, K, V> ExactSizeIterator for std::collections::hash_map::[ValuesMut](../../std/collections/hash%5Fmap/struct.ValuesMut.html "struct std::collections::hash_map::ValuesMut")<'a, K, V>
impl<'a, K, V> ExactSizeIterator for std::collections::hash_map::[Drain](../../std/collections/hash%5Fmap/struct.Drain.html "struct std::collections::hash_map::Drain")<'a, K, V>
impl<'a, K> ExactSizeIterator for std::collections::hash_set::[Iter](../../std/collections/hash%5Fset/struct.Iter.html "struct std::collections::hash_set::Iter")<'a, K>
impl<K> ExactSizeIterator for std::collections::hash_set::[IntoIter](../../std/collections/hash%5Fset/struct.IntoIter.html "struct std::collections::hash_set::IntoIter")<K>
impl<'a, K> ExactSizeIterator for std::collections::hash_set::[Drain](../../std/collections/hash%5Fset/struct.Drain.html "struct std::collections::hash_set::Drain")<'a, K>
impl ExactSizeIterator for [Args](../../std/env/struct.Args.html "struct std::env::Args")
impl ExactSizeIterator for [ArgsOs](../../std/env/struct.ArgsOs.html "struct std::env::ArgsOs")