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 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.
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
.
Basic usage:
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 {
fn len(&self) -> usize {
5 - self.count
}
}
let mut counter = Counter::new();
assert_eq!(5, counter.len());
let _ = counter.next();
assert_eq!(4, counter.len());
1.0.0 · Source
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.
Basic usage:
let mut range = 0..5;
assert_eq!(5, range.len());
let _ = range.next();
assert_eq!(4, range.len());
Source ð¬This is a nightly-only experimental API. (exact_size_is_empty
#35428)
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.
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);
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4