std::iter::repeat - Rust
Description: API documentation for the Rust `repeat` fn in crate `std`.
Keywords: rust, rustlang, rust-lang, repeat

# Function std::iter::repeat 1.0.0 [−] [src]

ⓘImportant traits for Repeat<A>

### Important traits for Repeat<A>

`impl<A> Iterator for Repeat<A> where
A: Clone, type Item = A;`

    pub fn repeat<T>(elt: T) -> Repeat<T> where
        T: Clone,

Creates a new iterator that endlessly repeats a single element.

The `repeat()` function repeats a single value over and over and over and over and over and 🔁.

Infinite iterators like `repeat()` are often used with adapters like `take`, in order to make them finite.

# Examples

Basic usage:

    use std::iter;

    // the number four 4ever:
    let mut fours = iter::repeat(4);

    assert_eq!(Some(4), fours.next());
    assert_eq!(Some(4), fours.next());
    assert_eq!(Some(4), fours.next());
    assert_eq!(Some(4), fours.next());
    assert_eq!(Some(4), fours.next());

    // yup, still four
    assert_eq!(Some(4), fours.next());Run

Going finite with `take`:

    use std::iter;

    // that last example was too many fours. Let's only have four fours.
    let mut four_fours = iter::repeat(4).take(4);

    assert_eq!(Some(4), four_fours.next());
    assert_eq!(Some(4), four_fours.next());
    assert_eq!(Some(4), four_fours.next());
    assert_eq!(Some(4), four_fours.next());

    // ... and now we're done
    assert_eq!(None, four_fours.next());Run

# 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
+ / -
  Collapse/expand 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`)