# Trait std::iter::ExactSizeIterator
1.0.0[−][src]

    pub trait ExactSizeIterator: Iterator {
        pub fn len(&self) -> usize { ... }
        pub 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 `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.

# Examples

Basic usage:

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

    assert_eq!(5, five.len());

Run

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());

Run

## Provided methods

### `pub fn len(&self) -> usize`[src]

Returns the exact 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 five = 0..5;

    assert_eq!(5, five.len());

Run

### `pub fn is_empty(&self) -> bool`[src]

🔬 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.

# 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);

Run
Loading content...

## Implementations on Foreign Types

### `impl<'a> ExactSizeIterator for EscapeAscii<'a>`[src]

Loading content...

## Implementors

### `impl ExactSizeIterator for std::ascii::EscapeDefault`[src]

### `impl ExactSizeIterator for EscapeDebug` 1.20.0[src]

### `impl ExactSizeIterator for std::char::EscapeDefault` 1.11.0[src]

#### `pub fn len(&self) -> usize`[src]

### `impl ExactSizeIterator for EscapeUnicode` 1.11.0[src]

#### `pub fn len(&self) -> usize`[src]

### `impl ExactSizeIterator for ToLowercase` 1.35.0[src]

### `impl ExactSizeIterator for ToUppercase` 1.35.0[src]

### `impl ExactSizeIterator for Args`[src]

#### `fn len(&self) -> usize`[src]

#### `fn is_empty(&self) -> bool`[src]

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

### `impl ExactSizeIterator for ArgsOs`[src]

#### `fn len(&self) -> usize`[src]

#### `fn is_empty(&self) -> bool`[src]

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

### `impl ExactSizeIterator for Range<i8>`[src]

### `impl ExactSizeIterator for Range<i16>`[src]

### `impl ExactSizeIterator for Range<i32>`[src]

### `impl ExactSizeIterator for Range<isize>`[src]

### `impl ExactSizeIterator for Range<u8>`[src]

### `impl ExactSizeIterator for Range<u16>`[src]

### `impl ExactSizeIterator for Range<u32>`[src]

### `impl ExactSizeIterator for Range<usize>`[src]

### `impl ExactSizeIterator for RangeInclusive<i8>`1.26.0[src]

### `impl ExactSizeIterator for RangeInclusive<i16>`1.26.0[src]

### `impl ExactSizeIterator for RangeInclusive<u8>`1.26.0[src]

### `impl ExactSizeIterator for RangeInclusive<u16>`1.26.0[src]

### `impl<'_> ExactSizeIterator for Bytes<'_>`[src]

#### `pub fn len(&self) -> usize`[src]

#### `pub fn is_empty(&self) -> bool`[src]

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

### `impl<'_, A> ExactSizeIterator for std::option::Iter<'_, A>`[src]

### `impl<'_, A> ExactSizeIterator for std::option::IterMut<'_, A>`[src]

### `impl<'_, I> ExactSizeIterator for &'_ mut I where
I: ExactSizeIterator + ?Sized,` [src]

#### `pub fn len(&self) -> usize`[src]

#### `pub fn is_empty(&self) -> bool`[src]

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

### `impl<'_, I, A> ExactSizeIterator for Splice<'_, I, A> where
I: Iterator,
A: Allocator,` 1.21.0[src]

### `impl<'_, K, V> ExactSizeIterator for std::collections::btree_map::Iter<'_, K, V>`[src]

#### `pub fn len(&self) -> usize`[src]

### `impl<'_, K, V> ExactSizeIterator for std::collections::btree_map::IterMut<'_, K, V>`[src]

#### `pub fn len(&self) -> usize`[src]

### `impl<'_, K, V> ExactSizeIterator for std::collections::btree_map::Keys<'_, K, V>`[src]

#### `pub fn len(&self) -> usize`[src]

### `impl<'_, K, V> ExactSizeIterator for std::collections::btree_map::Values<'_, K, V>`[src]

#### `pub fn len(&self) -> usize`[src]

### `impl<'_, K, V> ExactSizeIterator for std::collections::btree_map::ValuesMut<'_, K, V>`1.10.0[src]

#### `pub fn len(&self) -> usize`[src]

### `impl<'_, T> ExactSizeIterator for std::collections::binary_heap::Drain<'_, T>`1.6.0[src]

#### `pub fn is_empty(&self) -> bool`[src]

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

### `impl<'_, T> ExactSizeIterator for DrainSorted<'_, T> where
T: Ord,` [src]

### `impl<'_, T> ExactSizeIterator for std::collections::binary_heap::Iter<'_, T>`[src]

#### `pub fn is_empty(&self) -> bool`[src]

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

### `impl<'_, T> ExactSizeIterator for std::collections::btree_set::Iter<'_, T>`[src]

#### `pub fn len(&self) -> usize`[src]

### `impl<'_, T> ExactSizeIterator for std::collections::linked_list::Iter<'_, T>`[src]

### `impl<'_, T> ExactSizeIterator for std::collections::linked_list::IterMut<'_, T>`[src]

### `impl<'_, T> ExactSizeIterator for std::collections::vec_deque::Drain<'_, T>`1.6.0[src]

### `impl<'_, T> ExactSizeIterator for std::collections::vec_deque::Iter<'_, T>`[src]

#### `pub fn is_empty(&self) -> bool`[src]

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

### `impl<'_, T> ExactSizeIterator for std::collections::vec_deque::IterMut<'_, T>`[src]

#### `pub fn is_empty(&self) -> bool`[src]

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

### `impl<'_, T> ExactSizeIterator for std::result::Iter<'_, T>`[src]

### `impl<'_, T> ExactSizeIterator for std::result::IterMut<'_, T>`[src]

### `impl<'_, T> ExactSizeIterator for Chunks<'_, T>`[src]

### `impl<'_, T> ExactSizeIterator for ChunksExact<'_, T>`1.31.0[src]

#### `pub fn is_empty(&self) -> bool`[src]

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

### `impl<'_, T> ExactSizeIterator for ChunksExactMut<'_, T>`1.31.0[src]

#### `pub fn is_empty(&self) -> bool`[src]

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

### `impl<'_, T> ExactSizeIterator for ChunksMut<'_, T>`[src]

### `impl<'_, T> ExactSizeIterator for std::slice::Iter<'_, T>`[src]

#### `pub fn len(&self) -> usize`[src]

#### `pub fn is_empty(&self) -> bool`[src]

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

### `impl<'_, T> ExactSizeIterator for std::slice::IterMut<'_, T>`[src]

#### `pub fn len(&self) -> usize`[src]

#### `pub fn is_empty(&self) -> bool`[src]

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

### `impl<'_, T> ExactSizeIterator for RChunks<'_, T>`1.31.0[src]

### `impl<'_, T> ExactSizeIterator for RChunksExactMut<'_, T>`1.31.0[src]

#### `pub fn is_empty(&self) -> bool`[src]

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

### `impl<'_, T> ExactSizeIterator for RChunksMut<'_, T>`1.31.0[src]

### `impl<'_, T> ExactSizeIterator for Windows<'_, T>`[src]

### `impl<'_, T, A> ExactSizeIterator for std::vec::Drain<'_, T, A> where
A: Allocator,` 1.6.0[src]

#### `pub fn is_empty(&self) -> bool`[src]

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

### `impl<'_, T, const N: usize> ExactSizeIterator for ArrayChunks<'_, T, N>`[src]

#### `pub fn is_empty(&self) -> bool`[src]

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

### `impl<'_, T, const N: usize> ExactSizeIterator for ArrayChunksMut<'_, T, N>`[src]

#### `pub fn is_empty(&self) -> bool`[src]

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

### `impl<'_, T, const N: usize> ExactSizeIterator for ArrayWindows<'_, T, N>`[src]

#### `pub fn is_empty(&self) -> bool`[src]

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

### `impl<'a> ExactSizeIterator for CommandArgs<'a>`[src]

#### `fn len(&self) -> usize`[src]

#### `fn is_empty(&self) -> bool`[src]

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

### `impl<'a> ExactSizeIterator for CommandEnvs<'a>`[src]

#### `fn len(&self) -> usize`[src]

#### `fn is_empty(&self) -> bool`[src]

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

### `impl<'a, I, T> ExactSizeIterator for Cloned<I> where
T: 'a + Clone,
I: ExactSizeIterator<Item = &'a T>,` 1.1.0[src]

#### `pub fn len(&self) -> usize`[src]

#### `pub fn is_empty(&self) -> bool`[src]

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

### `impl<'a, I, T> ExactSizeIterator for Copied<I> where
T: 'a + Copy,
I: ExactSizeIterator<Item = &'a T>,` 1.36.0[src]

#### `pub fn len(&self) -> usize`[src]

#### `pub fn is_empty(&self) -> bool`[src]

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

### `impl<'a, T> ExactSizeIterator for RChunksExact<'a, T>`1.31.0[src]

#### `pub fn is_empty(&self) -> bool`[src]

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

### `impl<A> ExactSizeIterator for std::option::IntoIter<A>`[src]

### `impl<A, B> ExactSizeIterator for Zip<A, B> where
B: ExactSizeIterator,
A: ExactSizeIterator,` [src]

### `impl<A, F> ExactSizeIterator for OnceWith<F> where
F: FnOnce() -> A,` 1.43.0[src]

#### `pub fn len(&self) -> usize`[src]

### `impl<B, I, F> ExactSizeIterator for Map<I, F> where
F: FnMut(<I as Iterator>::Item) -> B,
I: ExactSizeIterator,` [src]

#### `pub fn len(&self) -> usize`[src]

#### `pub fn is_empty(&self) -> bool`[src]

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

### `impl<I> ExactSizeIterator for Enumerate<I> where
I: ExactSizeIterator,` [src]

#### `pub fn len(&self) -> usize`[src]

#### `pub fn is_empty(&self) -> bool`[src]

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

### `impl<I> ExactSizeIterator for Fuse<I> where
I: ExactSizeIterator,` [src]

#### `pub fn len(&self) -> usize`[src]

#### `pub fn is_empty(&self) -> bool`[src]

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

### `impl<I> ExactSizeIterator for Peekable<I> where
I: ExactSizeIterator,` [src]

### `impl<I> ExactSizeIterator for Rev<I> where
I: ExactSizeIterator + DoubleEndedIterator,` [src]

#### `pub fn len(&self) -> usize`[src]

#### `pub fn is_empty(&self) -> bool`[src]

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

### `impl<I> ExactSizeIterator for Skip<I> where
I: ExactSizeIterator,` [src]

### `impl<I> ExactSizeIterator for StepBy<I> where
I: ExactSizeIterator,` 1.28.0[src]

### `impl<I> ExactSizeIterator for Take<I> where
I: ExactSizeIterator,` [src]

### `impl<I, A> ExactSizeIterator for Box<I, A> where
I: ExactSizeIterator + ?Sized,
A: Allocator,` [src]

#### `pub fn len(&self) -> usize`[src]

#### `pub fn is_empty(&self) -> bool`[src]

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

### `impl<I, F> ExactSizeIterator for Inspect<I, F> where
F: FnMut(&<I as Iterator>::Item),
I: ExactSizeIterator,` [src]

#### `pub fn len(&self) -> usize`[src]

#### `pub fn is_empty(&self) -> bool`[src]

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

### `impl<K> ExactSizeIterator for std::collections::hash_set::Drain<'_, K>`[src]

#### `fn len(&self) -> usize`[src]

### `impl<K> ExactSizeIterator for std::collections::hash_set::IntoIter<K>`[src]

#### `fn len(&self) -> usize`[src]

### `impl<K> ExactSizeIterator for std::collections::hash_set::Iter<'_, K>`[src]

#### `fn len(&self) -> usize`[src]

### `impl<K, V> ExactSizeIterator for std::collections::btree_map::IntoIter<K, V>`[src]

#### `pub fn len(&self) -> usize`[src]

### `impl<K, V> ExactSizeIterator for std::collections::btree_map::IntoKeys<K, V>`[src]

#### `pub fn len(&self) -> usize`[src]

### `impl<K, V> ExactSizeIterator for std::collections::btree_map::IntoValues<K, V>`[src]

#### `pub fn len(&self) -> usize`[src]

### `impl<K, V> ExactSizeIterator for std::collections::hash_map::Drain<'_, K, V>`1.6.0[src]

#### `fn len(&self) -> usize`[src]

### `impl<K, V> ExactSizeIterator for std::collections::hash_map::IntoIter<K, V>`[src]

#### `fn len(&self) -> usize`[src]

### `impl<K, V> ExactSizeIterator for std::collections::hash_map::IntoKeys<K, V>`[src]

#### `fn len(&self) -> usize`[src]

### `impl<K, V> ExactSizeIterator for std::collections::hash_map::IntoValues<K, V>`[src]

#### `fn len(&self) -> usize`[src]

### `impl<K, V> ExactSizeIterator for std::collections::hash_map::Iter<'_, K, V>`[src]

#### `fn len(&self) -> usize`[src]

### `impl<K, V> ExactSizeIterator for std::collections::hash_map::IterMut<'_, K, V>`[src]

#### `fn len(&self) -> usize`[src]

### `impl<K, V> ExactSizeIterator for std::collections::hash_map::Keys<'_, K, V>`[src]

#### `fn len(&self) -> usize`[src]

### `impl<K, V> ExactSizeIterator for std::collections::hash_map::Values<'_, K, V>`[src]

#### `fn len(&self) -> usize`[src]

### `impl<K, V> ExactSizeIterator for std::collections::hash_map::ValuesMut<'_, K, V>`1.10.0[src]

#### `fn len(&self) -> usize`[src]

### `impl<T> ExactSizeIterator for std::collections::binary_heap::IntoIter<T>`[src]

#### `pub fn is_empty(&self) -> bool`[src]

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

### `impl<T> ExactSizeIterator for IntoIterSorted<T> where
T: Ord,` [src]

### `impl<T> ExactSizeIterator for std::collections::btree_set::IntoIter<T>`[src]

#### `pub fn len(&self) -> usize`[src]

### `impl<T> ExactSizeIterator for std::collections::linked_list::IntoIter<T>`[src]

### `impl<T> ExactSizeIterator for std::collections::vec_deque::IntoIter<T>`[src]

#### `pub fn is_empty(&self) -> bool`[src]

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

### `impl<T> ExactSizeIterator for std::result::IntoIter<T>`[src]

### `impl<T> ExactSizeIterator for Empty<T>`1.2.0[src]

#### `pub fn len(&self) -> usize`[src]

### `impl<T> ExactSizeIterator for Once<T>`1.2.0[src]

#### `pub fn len(&self) -> usize`[src]

### `impl<T, A> ExactSizeIterator for std::vec::IntoIter<T, A> where
A: Allocator,` [src]

#### `pub fn is_empty(&self) -> bool`[src]

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

### `impl<T, const N: usize> ExactSizeIterator for std::array::IntoIter<T, N>`1.40.0[src]

#### `pub fn len(&self) -> usize`[src]

#### `pub fn is_empty(&self) -> bool`[src]

🔬 This is a nightly-only experimental API. (`exact_size_is_empty` #35428)
Loading content...