no_std_compat::iter

Trait ExactSizeIterator

1.0.0 · Source
pub trait ExactSizeIterator: Iterator {
    // Provided methods
    fn len(&self) -> usize { ... }
    fn is_empty(&self) -> bool { ... }
}
Expand description

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 implement Iterator. When doing so, the implementation of Iterator::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.

Note that this trait is a safe trait and as such does not and cannot guarantee that the returned length is correct. This means that unsafe code must not rely on the correctness of Iterator::size_hint. The unstable and unsafe TrustedLen trait gives this additional guarantee.

§When shouldn’t an adapter be ExactSizeIterator?

If an adapter makes an iterator longer, then it’s usually incorrect for that adapter to implement ExactSizeIterator. The inner exact-sized iterator might already be usize::MAX-long, and thus the length of the longer adapted iterator would no longer be exactly representable in usize.

This is why Chain<A, B> isn’t ExactSizeIterator, even when A and B are both ExactSizeIterator.

§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 can easily calculate the remaining number of iterations.
    fn len(&self) -> usize {
        5 - self.count
    }
}

// And now we can use it!

let mut counter = Counter::new();

assert_eq!(5, counter.len());
let _ = counter.next();
assert_eq!(4, counter.len());

Provided Methods§

1.0.0 · Source

fn len(&self) -> usize

Returns the exact remaining length of the iterator.

The implementation ensures that the iterator will return exactly len() more times a Some(T) value, before returning None. 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 Iterator::size_hint function.

§Examples

Basic usage:

// a finite range knows exactly how many times it will iterate
let mut range = 0..5;

assert_eq!(5, range.len());
let _ = range.next();
assert_eq!(4, range.len());
Source

fn is_empty(&self) -> bool

🔬This is a nightly-only experimental API. (exact_size_is_empty)

Returns true if the iterator is empty.

This method has a default implementation using ExactSizeIterator::len(), so you don’t need to implement it yourself.

§Examples

Basic usage:

#![feature(exact_size_is_empty)]

let mut one_element = std::iter::once(0);
assert!(!one_element.is_empty());

assert_eq!(one_element.next(), Some(0));
assert!(one_element.is_empty());

assert_eq!(one_element.next(), None);

Implementors§

1.0.0 · Source§

impl ExactSizeIterator for no_std_compat::ascii::EscapeDefault

1.20.0 · Source§

impl ExactSizeIterator for EscapeDebug

1.11.0 · Source§

impl ExactSizeIterator for no_std_compat::char::EscapeDefault

1.11.0 · Source§

impl ExactSizeIterator for EscapeUnicode

1.35.0 · Source§

impl ExactSizeIterator for ToLowercase

1.35.0 · Source§

impl ExactSizeIterator for ToUppercase

1.0.0 · Source§

impl ExactSizeIterator for Range<i8>

1.0.0 · Source§

impl ExactSizeIterator for Range<i16>

1.0.0 · Source§

impl ExactSizeIterator for Range<i32>

1.0.0 · Source§

impl ExactSizeIterator for Range<isize>

1.0.0 · Source§

impl ExactSizeIterator for Range<u8>

1.0.0 · Source§

impl ExactSizeIterator for Range<u16>

1.0.0 · Source§

impl ExactSizeIterator for Range<u32>

1.0.0 · Source§

impl ExactSizeIterator for Range<usize>

1.26.0 · Source§

impl ExactSizeIterator for RangeInclusive<i8>

1.26.0 · Source§

impl ExactSizeIterator for RangeInclusive<i16>

1.26.0 · Source§

impl ExactSizeIterator for RangeInclusive<u8>

1.26.0 · Source§

impl ExactSizeIterator for RangeInclusive<u16>

1.0.0 · Source§

impl ExactSizeIterator for Bytes<'_>

Source§

impl ExactSizeIterator for IterRange<i8>

Source§

impl ExactSizeIterator for IterRange<i16>

Source§

impl ExactSizeIterator for IterRange<isize>

Source§

impl ExactSizeIterator for IterRange<u8>

Source§

impl ExactSizeIterator for IterRange<u16>

Source§

impl ExactSizeIterator for IterRange<usize>

Source§

impl ExactSizeIterator for IterRangeInclusive<i8>

Source§

impl ExactSizeIterator for IterRangeInclusive<u8>

1.1.0 · Source§

impl<'a, I, T> ExactSizeIterator for Cloned<I>
where T: 'a + Clone, I: ExactSizeIterator<Item = &'a T>,

1.36.0 · Source§

impl<'a, I, T> ExactSizeIterator for Copied<I>
where T: 'a + Copy, I: ExactSizeIterator<Item = &'a T>,

1.31.0 · Source§

impl<'a, T> ExactSizeIterator for RChunksExact<'a, T>

1.0.0 · Source§

impl<A> ExactSizeIterator for no_std_compat::option::IntoIter<A>

1.0.0 · Source§

impl<A> ExactSizeIterator for no_std_compat::option::Iter<'_, A>

1.0.0 · Source§

impl<A> ExactSizeIterator for no_std_compat::option::IterMut<'_, A>

1.82.0 · Source§

impl<A> ExactSizeIterator for RepeatN<A>
where A: Clone,

1.0.0 · Source§

impl<A, B> ExactSizeIterator for Zip<A, B>

1.43.0 · Source§

impl<A, F> ExactSizeIterator for OnceWith<F>
where F: FnOnce() -> A,

1.0.0 · Source§

impl<B, I, F> ExactSizeIterator for Map<I, F>
where I: ExactSizeIterator, F: FnMut(<I as Iterator>::Item) -> B,

1.82.0 · Source§

impl<F, A> ExactSizeIterator for Take<RepeatWith<F>>
where F: FnMut() -> A,

1.0.0 · Source§

impl<I> ExactSizeIterator for &mut I

1.0.0 · Source§

impl<I> ExactSizeIterator for Enumerate<I>

1.0.0 · Source§

impl<I> ExactSizeIterator for Fuse<I>

1.0.0 · Source§

impl<I> ExactSizeIterator for Peekable<I>

1.0.0 · Source§

impl<I> ExactSizeIterator for Rev<I>

1.0.0 · Source§

impl<I> ExactSizeIterator for Skip<I>

1.28.0 · Source§

impl<I> ExactSizeIterator for StepBy<I>

1.0.0 · Source§

impl<I> ExactSizeIterator for Take<I>

1.0.0 · Source§

impl<I, F> ExactSizeIterator for Inspect<I, F>
where I: ExactSizeIterator, F: FnMut(&<I as Iterator>::Item),

Source§

impl<I, F, R, const N: usize> ExactSizeIterator for MapWindows<I, F, N>
where I: ExactSizeIterator, F: FnMut(&[<I as Iterator>::Item; N]) -> R,

Source§

impl<I, const N: usize> ExactSizeIterator for no_std_compat::iter::ArrayChunks<I, N>

1.0.0 · Source§

impl<T> ExactSizeIterator for no_std_compat::result::IntoIter<T>

1.0.0 · Source§

impl<T> ExactSizeIterator for no_std_compat::result::Iter<'_, T>

1.0.0 · Source§

impl<T> ExactSizeIterator for no_std_compat::result::IterMut<'_, T>

1.0.0 · Source§

impl<T> ExactSizeIterator for Chunks<'_, T>

1.31.0 · Source§

impl<T> ExactSizeIterator for ChunksExact<'_, T>

1.31.0 · Source§

impl<T> ExactSizeIterator for ChunksExactMut<'_, T>

1.0.0 · Source§

impl<T> ExactSizeIterator for ChunksMut<'_, T>

1.0.0 · Source§

impl<T> ExactSizeIterator for no_std_compat::slice::Iter<'_, T>

1.0.0 · Source§

impl<T> ExactSizeIterator for no_std_compat::slice::IterMut<'_, T>

1.31.0 · Source§

impl<T> ExactSizeIterator for RChunks<'_, T>

1.31.0 · Source§

impl<T> ExactSizeIterator for RChunksExactMut<'_, T>

1.31.0 · Source§

impl<T> ExactSizeIterator for RChunksMut<'_, T>

1.0.0 · Source§

impl<T> ExactSizeIterator for Windows<'_, T>

1.2.0 · Source§

impl<T> ExactSizeIterator for Empty<T>

1.2.0 · Source§

impl<T> ExactSizeIterator for Once<T>

1.82.0 · Source§

impl<T> ExactSizeIterator for Take<Repeat<T>>
where T: Clone,

1.40.0 · Source§

impl<T, const N: usize> ExactSizeIterator for no_std_compat::array::IntoIter<T, N>

Source§

impl<T, const N: usize> ExactSizeIterator for no_std_compat::slice::ArrayChunks<'_, T, N>

Source§

impl<T, const N: usize> ExactSizeIterator for ArrayChunksMut<'_, T, N>

Source§

impl<T, const N: usize> ExactSizeIterator for ArrayWindows<'_, T, N>