core::iter::ExactSizeIterator - Rust
Description: API documentation for the Rust `ExactSizeIterator` trait in crate `core`.
Keywords: rust, rustlang, rust-lang, ExactSizeIterator

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

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

# 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

### `fn len(&self) -> usize`

Returns the exact number of times the iterator will iterate.

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 `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

### `fn is_empty(&self) -> bool`

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

Returns whether the iterator is empty.

This method has a default implementation using `self.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

## Implementors

###

`impl ExactSizeIterator for core::ascii::EscapeDefault`

[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 EscapeDebug`

[src]

####

`fn len(&self) -> usize`

1.0.0
[src]

####

`fn is_empty(&self) -> bool`

[src]

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

###

`impl ExactSizeIterator for core::char::EscapeDefault`

[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 EscapeUnicode`

[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]

####

`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<i16>`

[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<i32>`

[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<isize>`

[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<u8>`

[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<u16>`

[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<u32>`

[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<usize>`

[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 RangeInclusive<i8>`

[src]

####

`fn len(&self) -> usize`

1.0.0
[src]

####

`fn is_empty(&self) -> bool`

[src]

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

###

`impl ExactSizeIterator for RangeInclusive<i16>`

[src]

####

`fn len(&self) -> usize`

1.0.0
[src]

####

`fn is_empty(&self) -> bool`

[src]

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

###

`impl ExactSizeIterator for RangeInclusive<u8>`

[src]

####

`fn len(&self) -> usize`

1.0.0
[src]

####

`fn is_empty(&self) -> bool`

[src]

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

###

`impl ExactSizeIterator for RangeInclusive<u16>`

[src]

####

`fn len(&self) -> usize`

1.0.0
[src]

####

`fn is_empty(&self) -> bool`

[src]

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

###

`impl<'a> ExactSizeIterator for Bytes<'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, A> ExactSizeIterator for core::option::Iter<'a, 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, A> ExactSizeIterator for core::option::IterMut<'a, 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: 'a> ExactSizeIterator for Cloned<I> where I: ExactSizeIterator<Item = &'a T>, T: Clone,`

[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: ExactSizeIterator + ?Sized> ExactSizeIterator for &'a mut I`

[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, T> ExactSizeIterator for core::result::Iter<'a, T>`

[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, T> ExactSizeIterator for core::result::IterMut<'a, T>`

[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, T> ExactSizeIterator for Chunks<'a, T>`

[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, T> ExactSizeIterator for ChunksExact<'a, T>`

[src]

####

`fn is_empty(&self) -> bool`

[src]

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

####

`fn len(&self) -> usize`

1.0.0
[src]

###

`impl<'a, T> ExactSizeIterator for ChunksExactMut<'a, T>`

[src]

####

`fn is_empty(&self) -> bool`

[src]

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

####

`fn len(&self) -> usize`

1.0.0
[src]

###

`impl<'a, T> ExactSizeIterator for ChunksMut<'a, T>`

[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, T> ExactSizeIterator for core::slice::Iter<'a, T>`

[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, T> ExactSizeIterator for core::slice::IterMut<'a, T>`

[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, T> ExactSizeIterator for Windows<'a, T>`

[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 core::option::IntoIter<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, B> ExactSizeIterator for Zip<A, B> where A: ExactSizeIterator, B: ExactSizeIterator,`

[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<B, I: ExactSizeIterator, F> ExactSizeIterator for Map<I, F> where F: FnMut(I::Item) -> B,`

[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<I> ExactSizeIterator for Enumerate<I> where I: ExactSizeIterator,`

[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<I> ExactSizeIterator for Fuse<I> where I: ExactSizeIterator,`

[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<I> ExactSizeIterator for Rev<I> where I: ExactSizeIterator + DoubleEndedIterator,`

[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<I> ExactSizeIterator for Skip<I> where I: ExactSizeIterator,`

[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<I> ExactSizeIterator for StepBy<I> where I: ExactSizeIterator,`

[src]

####

`fn len(&self) -> usize`

1.0.0
[src]

####

`fn is_empty(&self) -> bool`

[src]

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

###

`impl<I> ExactSizeIterator for Take<I> where I: ExactSizeIterator,`

[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<I: ExactSizeIterator> ExactSizeIterator for Peekable<I>`

[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<I: ExactSizeIterator, F> ExactSizeIterator for Inspect<I, F> where F: FnMut(&I::Item),`

[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<T> ExactSizeIterator for Empty<T>`

[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<T> ExactSizeIterator for Once<T>`

[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<T> ExactSizeIterator for core::result::IntoIter<T>`

[src]

####

`fn len(&self) -> usize`

[src]

####

`fn is_empty(&self) -> bool`

[src]

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

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g. `str,u8` or `String,struct:Vec,test`)