pub trait ExactSizeIterator: Iterator {
// Provided methods
fn len(&self) -> usize ⓘ { ... }
fn is_empty(&self) -> bool { ... }
}
std
only.Expand description
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 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 · Sourcefn len(&self) -> usize ⓘ
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());
Sourcefn is_empty(&self) -> bool
🔬This is a nightly-only experimental API. (exact_size_is_empty
)
fn is_empty(&self) -> bool
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§
impl ExactSizeIterator for devela::_core::ascii::EscapeDefault
impl ExactSizeIterator for EscapeDebug
impl ExactSizeIterator for devela::_core::char::EscapeDefault
impl ExactSizeIterator for EscapeUnicode
impl ExactSizeIterator for ToLowercase
impl ExactSizeIterator for ToUppercase
impl ExactSizeIterator for IterRange<i8>
impl ExactSizeIterator for IterRange<i16>
impl ExactSizeIterator for IterRange<isize>
impl ExactSizeIterator for IterRange<u8>
impl ExactSizeIterator for IterRange<u16>
impl ExactSizeIterator for IterRange<usize>
impl ExactSizeIterator for IterRangeInclusive<i8>
impl ExactSizeIterator for IterRangeInclusive<u8>
impl ExactSizeIterator for Bytes<'_>
impl ExactSizeIterator for Range<i8>
impl ExactSizeIterator for Range<i16>
impl ExactSizeIterator for Range<i32>
impl ExactSizeIterator for Range<isize>
impl ExactSizeIterator for Range<u8>
impl ExactSizeIterator for Range<u16>
impl ExactSizeIterator for Range<u32>
impl ExactSizeIterator for Range<usize>
impl ExactSizeIterator for RangeInclusive<i8>
impl ExactSizeIterator for RangeInclusive<i16>
impl ExactSizeIterator for RangeInclusive<u8>
impl ExactSizeIterator for RangeInclusive<u16>
impl ExactSizeIterator for ArrayIntoIter
impl ExactSizeIterator for ArrayIter<'_>
impl ExactSizeIterator for BorrowedTupleIterator<'_, '_>
impl ExactSizeIterator for BoundDictIterator<'_>
impl ExactSizeIterator for BoundFrozenSetIterator<'_>
impl ExactSizeIterator for BoundListIterator<'_>
impl ExactSizeIterator for BoundSetIterator<'_>
impl ExactSizeIterator for BoundTupleIterator<'_>
impl ExactSizeIterator for Args
impl ExactSizeIterator for ArgsOs
impl<'a> ExactSizeIterator for CommandArgs<'a>
impl<'a> ExactSizeIterator for CommandEnvs<'a>
impl<'a, 'bump, I> ExactSizeIterator for devela::_dep::bumpalo::collections::vec::Splice<'a, 'bump, I>where
I: Iterator,
impl<'a, 'bump, T> ExactSizeIterator for devela::_dep::bumpalo::collections::vec::Drain<'a, 'bump, T>
impl<'a, I> ExactSizeIterator for devela::_dep::bumpalo::boxed::Box<'a, I>where
I: ExactSizeIterator + ?Sized,
impl<'a, I, T> ExactSizeIterator for Cloned<I>
impl<'a, I, T> ExactSizeIterator for Copied<I>
impl<'a, T> ExactSizeIterator for RChunksExact<'a, T>
impl<'a, T> ExactSizeIterator for Drain<'a, T>where
T: Array,
impl<'a, T, const CAP: usize> ExactSizeIterator for arrayvec::arrayvec::Drain<'a, T, CAP>where
T: 'a,
impl<'bump, T> ExactSizeIterator for devela::_dep::bumpalo::collections::vec::IntoIter<'bump, T>where
T: 'bump,
impl<'c, 'h> ExactSizeIterator for SubCaptureMatches<'c, 'h>
impl<'r> ExactSizeIterator for CaptureNames<'r>
impl<'s, T, const CAP: usize, S: Storage> ExactSizeIterator for DestaqueIter<'s, T, CAP, u8, S>
impl<'s, T, const CAP: usize, S: Storage> ExactSizeIterator for DestaqueIter<'s, T, CAP, u16, S>
impl<'s, T, const CAP: usize, S: Storage> ExactSizeIterator for DestaqueIter<'s, T, CAP, u32, S>
impl<'s, T, const CAP: usize, S: Storage> ExactSizeIterator for DestaqueIter<'s, T, CAP, usize, S>
impl<'s, T, const CAP: usize, S: Storage> ExactSizeIterator for StackIter<'s, T, CAP, u8, S>
impl<'s, T, const CAP: usize, S: Storage> ExactSizeIterator for StackIter<'s, T, CAP, u16, S>
impl<'s, T, const CAP: usize, S: Storage> ExactSizeIterator for StackIter<'s, T, CAP, u32, S>
impl<'s, T, const CAP: usize, S: Storage> ExactSizeIterator for StackIter<'s, T, CAP, usize, S>
impl<A> ExactSizeIterator for RepeatN<A>where
A: Clone,
impl<A> ExactSizeIterator for devela::_core::option::IntoIter<A>
impl<A> ExactSizeIterator for devela::_core::option::Iter<'_, A>
impl<A> ExactSizeIterator for devela::_core::option::IterMut<'_, A>
impl<A> ExactSizeIterator for IntoIter<A>where
A: Array,
impl<A, B> ExactSizeIterator for Zip<A, B>where
A: ExactSizeIterator,
B: ExactSizeIterator,
impl<A, F> ExactSizeIterator for OnceWith<F>where
F: FnOnce() -> A,
impl<B, I, F> ExactSizeIterator for Map<I, F>
impl<BUF: DstBuf, DST> ExactSizeIterator for DstValue<DST, BUF>where
DST: ExactSizeIterator + ?Sized,
unsafe_layout
only.impl<F, A> ExactSizeIterator for Take<RepeatWith<F>>where
F: FnMut() -> A,
impl<I1, I2> ExactSizeIterator for Mix<I1, I2>where
I1: Source + ExactSizeIterator,
<I1 as Iterator>::Item: FromSample<<I2 as Iterator>::Item> + Sample,
I2: Source + ExactSizeIterator,
<I2 as Iterator>::Item: Sample,
impl<I> ExactSizeIterator for &mut Iwhere
I: ExactSizeIterator + ?Sized,
impl<I> ExactSizeIterator for Enumerate<I>where
I: ExactSizeIterator,
impl<I> ExactSizeIterator for Fuse<I>where
I: ExactSizeIterator,
impl<I> ExactSizeIterator for Peekable<I>where
I: ExactSizeIterator,
impl<I> ExactSizeIterator for Rev<I>where
I: ExactSizeIterator + DoubleEndedIterator,
impl<I> ExactSizeIterator for Skip<I>where
I: ExactSizeIterator,
impl<I> ExactSizeIterator for StepBy<I>where
I: ExactSizeIterator,
impl<I> ExactSizeIterator for Take<I>where
I: ExactSizeIterator,
impl<I> ExactSizeIterator for Amplify<I>
impl<I> ExactSizeIterator for AutomaticGainControl<I>
impl<I> ExactSizeIterator for BltFilter<I>
impl<I> ExactSizeIterator for ChannelVolume<I>
impl<I> ExactSizeIterator for FadeIn<I>
impl<I> ExactSizeIterator for FadeOut<I>
impl<I> ExactSizeIterator for LinearGainRamp<I>
impl<I> ExactSizeIterator for Spatial<I>
impl<I> ExactSizeIterator for Speed<I>
impl<I, A> ExactSizeIterator for devela::all::Box<I, A>
impl<I, A> ExactSizeIterator for devela::_dep::_alloc::vec::Splice<'_, I, A>
impl<I, D> ExactSizeIterator for SamplesConverter<I, D>where
I: Source + ExactSizeIterator,
<I as Iterator>::Item: Sample,
D: FromSample<<I as Iterator>::Item> + Sample,
impl<I, F> ExactSizeIterator for Inspect<I, F>
impl<I, F, R, const N: usize> ExactSizeIterator for MapWindows<I, F, N>
impl<I, const N: usize> ExactSizeIterator for devela::_core::iter::ArrayChunks<I, N>where
I: ExactSizeIterator,
impl<K> ExactSizeIterator for devela::_dep::hashbrown::hash_set::Iter<'_, K>
impl<K> ExactSizeIterator for devela::_dep::_std::collections::hash_set::Drain<'_, K>
impl<K> ExactSizeIterator for devela::_dep::_std::collections::hash_set::IntoIter<K>
impl<K> ExactSizeIterator for devela::_dep::_std::collections::hash_set::Iter<'_, K>
impl<K, A> ExactSizeIterator for devela::_dep::hashbrown::hash_set::Drain<'_, K, A>where
A: Allocator,
impl<K, A> ExactSizeIterator for devela::_dep::hashbrown::hash_set::IntoIter<K, A>where
A: Allocator,
impl<K, V> ExactSizeIterator for devela::_dep::_alloc::collections::btree_map::Iter<'_, K, V>
impl<K, V> ExactSizeIterator for devela::_dep::_alloc::collections::btree_map::IterMut<'_, K, V>
impl<K, V> ExactSizeIterator for devela::_dep::_alloc::collections::btree_map::Keys<'_, K, V>
impl<K, V> ExactSizeIterator for devela::_dep::_alloc::collections::btree_map::Values<'_, K, V>
impl<K, V> ExactSizeIterator for devela::_dep::_alloc::collections::btree_map::ValuesMut<'_, K, V>
impl<K, V> ExactSizeIterator for devela::_dep::hashbrown::hash_map::Iter<'_, K, V>
impl<K, V> ExactSizeIterator for devela::_dep::hashbrown::hash_map::IterMut<'_, K, V>
impl<K, V> ExactSizeIterator for devela::_dep::hashbrown::hash_map::Keys<'_, K, V>
impl<K, V> ExactSizeIterator for devela::_dep::hashbrown::hash_map::Values<'_, K, V>
impl<K, V> ExactSizeIterator for devela::_dep::hashbrown::hash_map::ValuesMut<'_, K, V>
impl<K, V> ExactSizeIterator for devela::_dep::rkyv::collections::swiss_table::index_map::Iter<'_, K, V>
impl<K, V> ExactSizeIterator for devela::_dep::rkyv::collections::swiss_table::index_map::Keys<'_, K, V>
impl<K, V> ExactSizeIterator for devela::_dep::rkyv::collections::swiss_table::index_map::Values<'_, K, V>
impl<K, V> ExactSizeIterator for devela::_dep::_std::collections::hash_map::Drain<'_, K, V>
impl<K, V> ExactSizeIterator for devela::_dep::_std::collections::hash_map::IntoIter<K, V>
impl<K, V> ExactSizeIterator for devela::_dep::_std::collections::hash_map::IntoKeys<K, V>
impl<K, V> ExactSizeIterator for devela::_dep::_std::collections::hash_map::IntoValues<K, V>
impl<K, V> ExactSizeIterator for devela::_dep::_std::collections::hash_map::Iter<'_, K, V>
impl<K, V> ExactSizeIterator for devela::_dep::_std::collections::hash_map::IterMut<'_, K, V>
impl<K, V> ExactSizeIterator for devela::_dep::_std::collections::hash_map::Keys<'_, K, V>
impl<K, V> ExactSizeIterator for devela::_dep::_std::collections::hash_map::Values<'_, K, V>
impl<K, V> ExactSizeIterator for devela::_dep::_std::collections::hash_map::ValuesMut<'_, K, V>
impl<K, V, A> ExactSizeIterator for devela::_dep::_alloc::collections::btree_map::IntoIter<K, V, A>
impl<K, V, A> ExactSizeIterator for devela::_dep::_alloc::collections::btree_map::IntoKeys<K, V, A>
impl<K, V, A> ExactSizeIterator for devela::_dep::_alloc::collections::btree_map::IntoValues<K, V, A>
impl<K, V, A> ExactSizeIterator for devela::_dep::hashbrown::hash_map::Drain<'_, K, V, A>where
A: Allocator,
impl<K, V, A> ExactSizeIterator for devela::_dep::hashbrown::hash_map::IntoIter<K, V, A>where
A: Allocator,
impl<K, V, A> ExactSizeIterator for devela::_dep::hashbrown::hash_map::IntoKeys<K, V, A>where
A: Allocator,
impl<K, V, A> ExactSizeIterator for devela::_dep::hashbrown::hash_map::IntoValues<K, V, A>where
A: Allocator,
impl<K, V, H> ExactSizeIterator for devela::_dep::rkyv::collections::swiss_table::map::Iter<'_, K, V, H>
impl<K, V, H> ExactSizeIterator for devela::_dep::rkyv::collections::swiss_table::map::IterMut<'_, K, V, H>
impl<K, V, H> ExactSizeIterator for devela::_dep::rkyv::collections::swiss_table::map::Keys<'_, K, V, H>
impl<K, V, H> ExactSizeIterator for devela::_dep::rkyv::collections::swiss_table::map::Values<'_, K, V, H>
impl<K, V, H> ExactSizeIterator for devela::_dep::rkyv::collections::swiss_table::map::ValuesMut<'_, K, V, H>
impl<L, R> ExactSizeIterator for Either<L, R>
impl<L, R> ExactSizeIterator for IterEither<L, R>where
L: ExactSizeIterator,
R: ExactSizeIterator,
impl<T> ExactSizeIterator for devela::_core::result::IntoIter<T>
impl<T> ExactSizeIterator for devela::_core::result::Iter<'_, T>
impl<T> ExactSizeIterator for devela::_core::result::IterMut<'_, T>
impl<T> ExactSizeIterator for Chunks<'_, T>
impl<T> ExactSizeIterator for ChunksExact<'_, T>
impl<T> ExactSizeIterator for ChunksExactMut<'_, T>
impl<T> ExactSizeIterator for ChunksMut<'_, T>
impl<T> ExactSizeIterator for devela::_core::slice::Iter<'_, T>
impl<T> ExactSizeIterator for devela::_core::slice::IterMut<'_, T>
impl<T> ExactSizeIterator for RChunks<'_, T>
impl<T> ExactSizeIterator for RChunksExactMut<'_, T>
impl<T> ExactSizeIterator for RChunksMut<'_, T>
impl<T> ExactSizeIterator for Windows<'_, T>
impl<T> ExactSizeIterator for Empty<T>
impl<T> ExactSizeIterator for Once<T>
impl<T> ExactSizeIterator for Take<Repeat<T>>where
T: Clone,
impl<T> ExactSizeIterator for devela::_dep::_alloc::collections::binary_heap::Iter<'_, T>
impl<T> ExactSizeIterator for devela::_dep::_alloc::collections::btree_set::Iter<'_, T>
impl<T> ExactSizeIterator for devela::_dep::_alloc::collections::linked_list::Iter<'_, T>
impl<T> ExactSizeIterator for devela::_dep::_alloc::collections::linked_list::IterMut<'_, T>
impl<T> ExactSizeIterator for devela::_dep::_alloc::collections::vec_deque::Iter<'_, T>
impl<T> ExactSizeIterator for devela::_dep::_alloc::collections::vec_deque::IterMut<'_, T>
impl<T> ExactSizeIterator for devela::_dep::hashbrown::hash_table::Iter<'_, T>
impl<T> ExactSizeIterator for devela::_dep::hashbrown::hash_table::IterMut<'_, T>
impl<T> ExactSizeIterator for RawIter<T>
impl<T> ExactSizeIterator for AncillaryIter<'_, T>
impl<T> ExactSizeIterator for IntoIter<T>where
T: Buf,
impl<T> ExactSizeIterator for ReadChunkIntoIter<'_, T>
impl<T, A> ExactSizeIterator for devela::_dep::_alloc::collections::binary_heap::Drain<'_, T, A>where
A: Allocator,
impl<T, A> ExactSizeIterator for DrainSorted<'_, T, A>
impl<T, A> ExactSizeIterator for devela::_dep::_alloc::collections::binary_heap::IntoIter<T, A>where
A: Allocator,
impl<T, A> ExactSizeIterator for IntoIterSorted<T, A>
impl<T, A> ExactSizeIterator for devela::_dep::_alloc::collections::btree_set::IntoIter<T, A>
impl<T, A> ExactSizeIterator for devela::_dep::_alloc::collections::linked_list::IntoIter<T, A>where
A: Allocator,
impl<T, A> ExactSizeIterator for devela::_dep::_alloc::collections::vec_deque::Drain<'_, T, A>where
A: Allocator,
impl<T, A> ExactSizeIterator for devela::_dep::_alloc::collections::vec_deque::IntoIter<T, A>where
A: Allocator,
impl<T, A> ExactSizeIterator for devela::_dep::_alloc::vec::Drain<'_, T, A>where
A: Allocator,
impl<T, A> ExactSizeIterator for devela::_dep::_alloc::vec::IntoIter<T, A>where
A: Allocator,
impl<T, A> ExactSizeIterator for devela::_dep::hashbrown::hash_table::Drain<'_, T, A>where
A: Allocator,
impl<T, A> ExactSizeIterator for devela::_dep::hashbrown::hash_table::IntoIter<T, A>where
A: Allocator,
impl<T, const CAP: usize> ExactSizeIterator for arrayvec::arrayvec::IntoIter<T, CAP>
impl<T, const N: usize> ExactSizeIterator for devela::_core::slice::ArrayChunks<'_, T, N>
impl<T, const N: usize> ExactSizeIterator for ArrayChunksMut<'_, T, N>
impl<T, const N: usize> ExactSizeIterator for ArrayWindows<'_, T, N>
impl<T, const N: usize> ExactSizeIterator for devela::all::ArrayIntoIter<T, N>
impl<_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11> ExactSizeIterator for TupleIter<_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11>
_tuple
only.impl<_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11> ExactSizeIterator for TupleIterMut<'_, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11>
_tuple
only.impl<_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11> ExactSizeIterator for TupleIterRef<'_, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11>
_tuple
only.