Trait std::iter::ExactSizeIterator
[−]
[src]
pub trait ExactSizeIterator: Iterator { fn len(&self) -> usize { ... } }
An iterator that knows its exact length.
Many Iterator
s 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 implement
Iterator
. 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.
Examples
Basic usage:
// a finite range knows exactly how many times it will iterate let five = 0..5; assert_eq!(5, five.len());
In the module level docs, we implemented an Iterator
,
Counter
. Let's implement ExactSizeIterator
for it as well:
impl ExactSizeIterator for Counter { // We already have the number of iterations, so we can use it directly. fn len(&self) -> usize { self.count } } // And now we can use it! let counter = Counter::new(); assert_eq!(0, counter.len());
Provided Methods
fn len(&self) -> usize
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_hint()
function.
Examples
Basic usage:
// a finite range knows exactly how many times it will iterate let five = 0..5; assert_eq!(5, five.len());
Implementors
impl<'a, I> ExactSizeIterator for &'a mut I where I: ExactSizeIterator + ?Sized
impl<I> ExactSizeIterator for Enumerate<I> where I: ExactSizeIterator
impl<I, F> ExactSizeIterator for Inspect<I, F> where I: ExactSizeIterator, F: FnMut(&I::Item) -> ()
impl<I> ExactSizeIterator for Rev<I> where I: ExactSizeIterator + DoubleEndedIterator
impl<B, I, F> ExactSizeIterator for Map<I, F> where F: FnMut(I::Item) -> B, I: ExactSizeIterator
impl<A, B> ExactSizeIterator for Zip<A, B> where B: ExactSizeIterator, A: ExactSizeIterator
impl<'a, I, T> ExactSizeIterator for Cloned<I> where I: ExactSizeIterator<Item=&'a T>, T: 'a + Clone
impl<I> ExactSizeIterator for Peekable<I> where I: ExactSizeIterator
impl<I> ExactSizeIterator for Skip<I> where I: ExactSizeIterator
impl<I> ExactSizeIterator for Take<I> where I: ExactSizeIterator
impl<I> ExactSizeIterator for Fuse<I> where I: ExactSizeIterator
impl ExactSizeIterator for Range<usize>
impl ExactSizeIterator for Range<u8>
impl ExactSizeIterator for Range<u16>
impl ExactSizeIterator for Range<u32>
impl ExactSizeIterator for Range<isize>
impl ExactSizeIterator for Range<i8>
impl ExactSizeIterator for Range<i16>
impl ExactSizeIterator for Range<i32>
impl<T> ExactSizeIterator for Empty<T>
impl<T> ExactSizeIterator for Once<T>
impl<A> ExactSizeIterator for Item<A>
impl<'a, A> ExactSizeIterator for Iter<'a, A>
impl<'a, A> ExactSizeIterator for IterMut<'a, A>
impl<A> ExactSizeIterator for IntoIter<A>
impl<'a, T> ExactSizeIterator for Iter<'a, T>
impl<'a, T> ExactSizeIterator for IterMut<'a, T>
impl<T> ExactSizeIterator for IntoIter<T>
impl<'a, T> ExactSizeIterator for Iter<'a, T>
impl<'a, T> ExactSizeIterator for IterMut<'a, T>
impl<'a, T> ExactSizeIterator for Windows<'a, T>
impl<'a, T> ExactSizeIterator for Chunks<'a, T>
impl<'a, T> ExactSizeIterator for ChunksMut<'a, T>
impl<'a> ExactSizeIterator for Bytes<'a>
impl<I> ExactSizeIterator for Box<I> where I: ExactSizeIterator + ?Sized
impl<'a, T> ExactSizeIterator for Iter<'a, T>
impl<T> ExactSizeIterator for IntoIter<T>
impl<'a, T> ExactSizeIterator for Drain<'a, T> where T: 'a
impl<'a, K, V> ExactSizeIterator for Iter<'a, K, V> where V: 'a, K: 'a
impl<'a, K, V> ExactSizeIterator for IterMut<'a, K, V> where V: 'a, K: 'a
impl<K, V> ExactSizeIterator for IntoIter<K, V>
impl<'a, K, V> ExactSizeIterator for Keys<'a, K, V>
impl<'a, K, V> ExactSizeIterator for Values<'a, K, V>
impl<'a, T> ExactSizeIterator for Iter<'a, T>
impl<T> ExactSizeIterator for IntoIter<T>
impl<'a, A> ExactSizeIterator for Iter<'a, A>
impl<'a, A> ExactSizeIterator for IterMut<'a, A>
impl<A> ExactSizeIterator for IntoIter<A>
impl<T> ExactSizeIterator for IntoIter<T>
impl<'a, T> ExactSizeIterator for Drain<'a, T>
impl<'a, T> ExactSizeIterator for Iter<'a, T>
impl<'a, T> ExactSizeIterator for IterMut<'a, T>
impl<T> ExactSizeIterator for IntoIter<T>
impl<'a, T> ExactSizeIterator for Drain<'a, T> where T: 'a
impl ExactSizeIterator for EscapeDefault
impl<'a, K, V> ExactSizeIterator for Iter<'a, K, V>
impl<'a, K, V> ExactSizeIterator for IterMut<'a, K, V>
impl<K, V> ExactSizeIterator for IntoIter<K, V>
impl<'a, K, V> ExactSizeIterator for Drain<'a, K, V>
impl<'a, K, V> ExactSizeIterator for Iter<'a, K, V>
impl<'a, K, V> ExactSizeIterator for IterMut<'a, K, V>
impl<K, V> ExactSizeIterator for IntoIter<K, V>
impl<'a, K, V> ExactSizeIterator for Keys<'a, K, V>
impl<'a, K, V> ExactSizeIterator for Values<'a, K, V>
impl<'a, K, V> ExactSizeIterator for Drain<'a, K, V>
impl<'a, K> ExactSizeIterator for Iter<'a, K>
impl<K> ExactSizeIterator for IntoIter<K>
impl<'a, K> ExactSizeIterator for Drain<'a, K>