]>
git.proxmox.com Git - rustc.git/blob - library/core/src/iter/traits/exact_size.rs
1 /// An iterator that knows its exact length.
3 /// Many [`Iterator`]s don't know how many times they will iterate, but some do.
4 /// If an iterator knows how many times it can iterate, providing access to
5 /// that information can be useful. For example, if you want to iterate
6 /// backwards, a good start is to know where the end is.
8 /// When implementing an `ExactSizeIterator`, you must also implement
9 /// [`Iterator`]. When doing so, the implementation of [`Iterator::size_hint`]
10 /// *must* return the exact size of the iterator.
12 /// The [`len`] method has a default implementation, so you usually shouldn't
13 /// implement it. However, you may be able to provide a more performant
14 /// implementation than the default, so overriding it in this case makes sense.
16 /// [`len`]: ExactSizeIterator::len
23 /// // a finite range knows exactly how many times it will iterate
26 /// assert_eq!(5, five.len());
29 /// In the [module level docs][moddocs], we implemented an [`Iterator`],
30 /// `Counter`. Let's implement `ExactSizeIterator` for it as well:
32 /// [moddocs]: index.html
35 /// # struct Counter {
39 /// # fn new() -> Counter {
40 /// # Counter { count: 0 }
43 /// # impl Iterator for Counter {
44 /// # type Item = usize;
45 /// # fn next(&mut self) -> Option<Self::Item> {
46 /// # self.count += 1;
47 /// # if self.count < 6 {
48 /// # Some(self.count)
54 /// impl ExactSizeIterator for Counter {
55 /// // We can easily calculate the remaining number of iterations.
56 /// fn len(&self) -> usize {
61 /// // And now we can use it!
63 /// let counter = Counter::new();
65 /// assert_eq!(5, counter.len());
67 #[stable(feature = "rust1", since = "1.0.0")]
68 pub trait ExactSizeIterator
: Iterator
{
69 /// Returns the exact length of the iterator.
71 /// The implementation ensures that the iterator will return exactly `len()`
72 /// more times a [`Some(T)`] value, before returning [`None`].
73 /// This method has a default implementation, so you usually should not
74 /// implement it directly. However, if you can provide a more efficient
75 /// implementation, you can do so. See the [trait-level] docs for an
78 /// This function has the same safety guarantees as the
79 /// [`Iterator::size_hint`] function.
81 /// [trait-level]: ExactSizeIterator
89 /// // a finite range knows exactly how many times it will iterate
92 /// assert_eq!(5, five.len());
95 #[stable(feature = "rust1", since = "1.0.0")]
96 fn len(&self) -> usize {
97 let (lower
, upper
) = self.size_hint();
98 // Note: This assertion is overly defensive, but it checks the invariant
99 // guaranteed by the trait. If this trait were rust-internal,
100 // we could use debug_assert!; assert_eq! will check all Rust user
101 // implementations too.
102 assert_eq
!(upper
, Some(lower
));
106 /// Returns `true` if the iterator is empty.
108 /// This method has a default implementation using
109 /// [`ExactSizeIterator::len()`], so you don't need to implement it yourself.
116 /// #![feature(exact_size_is_empty)]
118 /// let mut one_element = std::iter::once(0);
119 /// assert!(!one_element.is_empty());
121 /// assert_eq!(one_element.next(), Some(0));
122 /// assert!(one_element.is_empty());
124 /// assert_eq!(one_element.next(), None);
127 #[unstable(feature = "exact_size_is_empty", issue = "35428")]
128 fn is_empty(&self) -> bool
{
133 #[stable(feature = "rust1", since = "1.0.0")]
134 impl<I
: ExactSizeIterator
+ ?Sized
> ExactSizeIterator
for &mut I
{
135 fn len(&self) -> usize {
138 fn is_empty(&self) -> bool
{