<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="generator" content="rustdoc">
    <meta name="description" content="API documentation for the Rust `repeat` fn in crate `std`.">
    <meta name="keywords" content="rust, rustlang, rust-lang, repeat">

    <title>std::iter::repeat - Rust</title>

    <link rel="stylesheet" type="text/css" href="../../rustdoc.css">
    <link rel="stylesheet" type="text/css" href="../../main.css">

    <link rel="shortcut icon" href="https://doc.rust-lang.org/favicon.ico">
    
</head>
<body class="rustdoc">
    <!--[if lte IE 8]>
    <div class="warning">
        This old browser is unsupported and will most likely display funky
        things.
    </div>
    <![endif]-->

    

    <nav class="sidebar">
        <a href='../../std/index.html'><img src='https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png' alt='' width='100'></a>
        <p class='location'><a href='../index.html'>std</a>::<wbr><a href='index.html'>iter</a></p><script>window.sidebarCurrent = {name: 'repeat', ty: 'fn', relpath: ''};</script><script defer src="sidebar-items.js"></script>
    </nav>

    <nav class="sub">
        <form class="search-form js-only">
            <div class="search-container">
                <input class="search-input" name="search"
                       autocomplete="off"
                       placeholder="Click or press ‘S’ to search, ‘?’ for more options…"
                       type="search">
            </div>
        </form>
    </nav>

    <section id='main' class="content fn">
<h1 class='fqn'><span class='in-band'>Function <a href='../index.html'>std</a>::<wbr><a href='index.html'>iter</a>::<wbr><a class='fn' href=''>repeat</a></span><span class='out-of-band'><span id='render-detail'>
            <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">
                [<span class='inner'>&#x2212;</span>]
            </a>
        </span><a id='src-28892' class='srclink' href='../../core/iter/fn.repeat.html?gotosrc=28892' title='goto source code'>[src]</a></span></h1>
<pre class='rust fn'>pub fn repeat&lt;T&gt;(elt: T) -&gt; <a class='struct' href='../../std/iter/struct.Repeat.html' title='std::iter::Repeat'>Repeat</a>&lt;T&gt; <span class='where'>where T: <a class='trait' href='../../std/clone/trait.Clone.html' title='std::clone::Clone'>Clone</a></span></pre><div class='docblock'><p>Creates a new iterator that endlessly repeats a single element.</p>

<p>The <code>repeat()</code> function repeats a single value over and over and over and
over and over and 🔁.</p>

<p>Infinite iterators like <code>repeat()</code> are often used with adapters like
<a href="trait.Iterator.html#method.take"><code>take()</code></a>, in order to make them finite.</p>

<h1 id='examples' class='section-header'><a href='#examples'>Examples</a></h1>
<p>Basic usage:</p>
<span class='rusttest'>fn main() {
    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());
    
}</span><pre class='rust rust-example-rendered'>
<span class='kw'>use</span> <span class='ident'>std</span>::<span class='ident'>iter</span>;

<span class='comment'>// the number four 4ever:</span>
<span class='kw'>let</span> <span class='kw-2'>mut</span> <span class='ident'>fours</span> <span class='op'>=</span> <span class='ident'>iter</span>::<span class='ident'>repeat</span>(<span class='number'>4</span>);

<span class='macro'>assert_eq</span><span class='macro'>!</span>(<span class='prelude-val'>Some</span>(<span class='number'>4</span>), <span class='ident'>fours</span>.<span class='ident'>next</span>());
<span class='macro'>assert_eq</span><span class='macro'>!</span>(<span class='prelude-val'>Some</span>(<span class='number'>4</span>), <span class='ident'>fours</span>.<span class='ident'>next</span>());
<span class='macro'>assert_eq</span><span class='macro'>!</span>(<span class='prelude-val'>Some</span>(<span class='number'>4</span>), <span class='ident'>fours</span>.<span class='ident'>next</span>());
<span class='macro'>assert_eq</span><span class='macro'>!</span>(<span class='prelude-val'>Some</span>(<span class='number'>4</span>), <span class='ident'>fours</span>.<span class='ident'>next</span>());
<span class='macro'>assert_eq</span><span class='macro'>!</span>(<span class='prelude-val'>Some</span>(<span class='number'>4</span>), <span class='ident'>fours</span>.<span class='ident'>next</span>());

<span class='comment'>// yup, still four</span>
<span class='macro'>assert_eq</span><span class='macro'>!</span>(<span class='prelude-val'>Some</span>(<span class='number'>4</span>), <span class='ident'>fours</span>.<span class='ident'>next</span>());</pre>

<p>Going finite with <a href="trait.Iterator.html#method.take"><code>take()</code></a>:</p>
<span class='rusttest'>fn main() {
    use std::iter;
    
    // that last example was too many fours. Let&#39;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&#39;re done
    assert_eq!(None, four_fours.next());
    
}</span><pre class='rust rust-example-rendered'>
<span class='kw'>use</span> <span class='ident'>std</span>::<span class='ident'>iter</span>;

<span class='comment'>// that last example was too many fours. Let&#39;s only have four fours.</span>
<span class='kw'>let</span> <span class='kw-2'>mut</span> <span class='ident'>four_fours</span> <span class='op'>=</span> <span class='ident'>iter</span>::<span class='ident'>repeat</span>(<span class='number'>4</span>).<span class='ident'>take</span>(<span class='number'>4</span>);

<span class='macro'>assert_eq</span><span class='macro'>!</span>(<span class='prelude-val'>Some</span>(<span class='number'>4</span>), <span class='ident'>four_fours</span>.<span class='ident'>next</span>());
<span class='macro'>assert_eq</span><span class='macro'>!</span>(<span class='prelude-val'>Some</span>(<span class='number'>4</span>), <span class='ident'>four_fours</span>.<span class='ident'>next</span>());
<span class='macro'>assert_eq</span><span class='macro'>!</span>(<span class='prelude-val'>Some</span>(<span class='number'>4</span>), <span class='ident'>four_fours</span>.<span class='ident'>next</span>());
<span class='macro'>assert_eq</span><span class='macro'>!</span>(<span class='prelude-val'>Some</span>(<span class='number'>4</span>), <span class='ident'>four_fours</span>.<span class='ident'>next</span>());

<span class='comment'>// ... and now we&#39;re done</span>
<span class='macro'>assert_eq</span><span class='macro'>!</span>(<span class='prelude-val'>None</span>, <span class='ident'>four_fours</span>.<span class='ident'>next</span>());</pre>
</div></section>
    <section id='search' class="content hidden"></section>

    <section class="footer"></section>

    <aside id="help" class="hidden">
        <div>
            <h1 class="hidden">Help</h1>

            <div class="shortcuts">
                <h2>Keyboard Shortcuts</h2>

                <dl>
                    <dt>?</dt>
                    <dd>Show this help dialog</dd>
                    <dt>S</dt>
                    <dd>Focus the search field</dd>
                    <dt>&larrb;</dt>
                    <dd>Move up in search results</dd>
                    <dt>&rarrb;</dt>
                    <dd>Move down in search results</dd>
                    <dt>&#9166;</dt>
                    <dd>Go to active search result</dd>
                </dl>
            </div>

            <div class="infos">
                <h2>Search Tricks</h2>

                <p>
                    Prefix searches with a type followed by a colon (e.g.
                    <code>fn:</code>) to restrict the search to a given type.
                </p>

                <p>
                    Accepted types are: <code>fn</code>, <code>mod</code>,
                    <code>struct</code>, <code>enum</code>,
                    <code>trait</code>, <code>type</code>, <code>macro</code>,
                    and <code>const</code>.
                </p>

                <p>
                    Search functions by type signature (e.g.
                    <code>vec -> usize</code>)
                </p>
            </div>
        </div>
    </aside>

    

    <script>
        window.rootPath = "../../";
        window.currentCrate = "std";
        window.playgroundUrl = "https://play.rust-lang.org/";
    </script>
    <script src="../../jquery.js"></script>
    <script src="../../main.js"></script>
    <script src="../../playpen.js"></script>
    <script defer src="../../search-index.js"></script>
</body>
</html>