pub trait ExactSizeIterator: Iterator { fn len(&self) -> usize { ... } fn is_empty(&self) -> bool { ... } }
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.
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 can easily calculate the remaining number of iterations. fn len(&self) -> usize { 5 - self.count } } // And now we can use it! let counter = Counter::new(); assert_eq!(5, counter.len());
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.
Basic usage:
// a finite range knows exactly how many times it will iterate let five = 0..5; assert_eq!(5, five.len());
fn is_empty(&self) -> bool
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);
impl<'a, K, V> ExactSizeIterator for std::collections::btree_map::Values<'a, K, V>
impl<'a, T> ExactSizeIterator for std::collections::vec_deque::Iter<'a, T>
impl<'a, T> ExactSizeIterator for std::collections::binary_heap::Drain<'a, T> where
    T: 'a,Â
impl<'a, K, V> ExactSizeIterator for std::collections::btree_map::Keys<'a, K, V>
impl<K, V> ExactSizeIterator for std::collections::btree_map::IntoIter<K, V>
impl<I> ExactSizeIterator for Box<I> where
    I: ExactSizeIterator + ?Sized,Â
impl<'a, T> ExactSizeIterator for std::collections::linked_list::IterMut<'a, T>
impl<'a, T> ExactSizeIterator for std::collections::vec_deque::IterMut<'a, T>
impl<T> ExactSizeIterator for std::collections::vec_deque::IntoIter<T>
impl<'a, T> ExactSizeIterator for std::collections::binary_heap::Iter<'a, T>
impl<T> ExactSizeIterator for std::collections::linked_list::IntoIter<T>
impl<'a, K, V> ExactSizeIterator for std::collections::btree_map::Iter<'a, K, V> where
    K: 'a,
    V: 'a,Â
impl<'a, I> ExactSizeIterator for Splice<'a, I> where
    I: Iterator,Â
impl<T> ExactSizeIterator for std::collections::binary_heap::IntoIter<T>
impl<'a, T> ExactSizeIterator for std::collections::linked_list::Iter<'a, T>
impl<T> ExactSizeIterator for std::vec::IntoIter<T>
impl<T> ExactSizeIterator for std::collections::btree_set::IntoIter<T>
impl<'a, T> ExactSizeIterator for std::collections::btree_set::Iter<'a, T>
impl<'a, K, V> ExactSizeIterator for std::collections::btree_map::IterMut<'a, K, V> where
    K: 'a,
    V: 'a,Â
impl<'a, T> ExactSizeIterator for std::vec::Drain<'a, T>
impl<'a, T> ExactSizeIterator for std::collections::vec_deque::Drain<'a, T> where
    T: 'a,Â
impl<'a, K, V> ExactSizeIterator for std::collections::btree_map::ValuesMut<'a, K, V>
impl ExactSizeIterator for RangeInclusive<i8>
impl<I> ExactSizeIterator for StepBy<I> where
    I: ExactSizeIterator,Â
impl ExactSizeIterator for Range<usize>
impl<T> ExactSizeIterator for Once<T>
impl<A, B> ExactSizeIterator for Zip<A, B> where
    A: ExactSizeIterator,
    B: ExactSizeIterator,Â
impl<I> ExactSizeIterator for Fuse<I> where
    I: ExactSizeIterator,Â
impl<'a, T> ExactSizeIterator for std::result::Iter<'a, T>
impl<'a, A> ExactSizeIterator for std::option::IterMut<'a, A>
impl ExactSizeIterator for Range<u32>
impl<'a, T> ExactSizeIterator for std::slice::IterMut<'a, T>
impl ExactSizeIterator for Range<u8>
impl<'a, A> ExactSizeIterator for std::option::Iter<'a, A>
impl ExactSizeIterator for Range<i32>
impl ExactSizeIterator for Range<u16>
impl<A> ExactSizeIterator for std::option::IntoIter<A>
impl<B, I, F> ExactSizeIterator for Map<I, F> where
    F: FnMut(<I as Iterator>::Item) -> B,
    I: ExactSizeIterator,Â
impl<I> ExactSizeIterator for Take<I> where
    I: ExactSizeIterator,Â
impl ExactSizeIterator for std::char::EscapeDefault
impl<'a> ExactSizeIterator for Bytes<'a>
impl ExactSizeIterator for RangeInclusive<i16>
impl<'a, T> ExactSizeIterator for Windows<'a, T>
impl ExactSizeIterator for Range<isize>
impl<'a, T> ExactSizeIterator for Chunks<'a, T>
impl<'a, T> ExactSizeIterator for std::result::IterMut<'a, T>
impl ExactSizeIterator for RangeInclusive<u16>
impl<I> ExactSizeIterator for Skip<I> where
    I: ExactSizeIterator,Â
impl<I, F> ExactSizeIterator for Inspect<I, F> where
    F: FnMut(&<I as Iterator>::Item) -> (),
    I: ExactSizeIterator,Â
impl<'a, I, T> ExactSizeIterator for Cloned<I> where
    I: ExactSizeIterator<Item = &'a T>,
    T: 'a + Clone,Â
impl ExactSizeIterator for EscapeUnicode
impl<T> ExactSizeIterator for Empty<T>
impl<'a, T> ExactSizeIterator for std::slice::Iter<'a, T>
impl<I> ExactSizeIterator for Peekable<I> where
    I: ExactSizeIterator,Â
impl<I> ExactSizeIterator for Enumerate<I> where
    I: ExactSizeIterator,Â
impl ExactSizeIterator for Range<i8>
impl ExactSizeIterator for RangeInclusive<u8>
impl<I> ExactSizeIterator for Rev<I> where
    I: ExactSizeIterator + DoubleEndedIterator,Â
impl ExactSizeIterator for Range<i16>
impl<'a, T> ExactSizeIterator for ChunksMut<'a, T>
impl ExactSizeIterator for EscapeDebug
impl<'a, I> ExactSizeIterator for &'a mut I where
    I: ExactSizeIterator + ?Sized,Â
impl<T> ExactSizeIterator for std::result::IntoIter<T>
impl ExactSizeIterator for std::ascii::EscapeDefault
impl<'a, K, V> ExactSizeIterator for std::collections::hash_map::Iter<'a, K, V>
impl<'a, K, V> ExactSizeIterator for std::collections::hash_map::IterMut<'a, K, V>
impl<K, V> ExactSizeIterator for std::collections::hash_map::IntoIter<K, V>
impl<'a, K, V> ExactSizeIterator for std::collections::hash_map::Keys<'a, K, V>
impl<'a, K, V> ExactSizeIterator for std::collections::hash_map::Values<'a, K, V>
impl<'a, K, V> ExactSizeIterator for std::collections::hash_map::ValuesMut<'a, K, V>
impl<'a, K, V> ExactSizeIterator for std::collections::hash_map::Drain<'a, K, V>
impl<'a, K> ExactSizeIterator for std::collections::hash_set::Iter<'a, K>
impl<K> ExactSizeIterator for std::collections::hash_set::IntoIter<K>
impl<'a, K> ExactSizeIterator for std::collections::hash_set::Drain<'a, K>
impl ExactSizeIterator for Args
impl ExactSizeIterator for ArgsOs
© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/iter/trait.ExactSizeIterator.html