Sophie

Sophie

distrib > Mageia > 6 > armv7hl > media > core-updates > by-pkgid > 4e2dbb669434a7691662cb2f0ad38972 > files > 9639

rust-doc-1.28.0-1.mga6.armv7hl.rpm

<!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_with` fn in crate `core`."><meta name="keywords" content="rust, rustlang, rust-lang, repeat_with"><title>core::iter::repeat_with - Rust</title><link rel="stylesheet" type="text/css" href="../../normalize.css"><link rel="stylesheet" type="text/css" href="../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../../dark.css"><link rel="stylesheet" type="text/css" href="../../light.css" id="themeStyle"><script src="../../storage.js"></script><link rel="shortcut icon" href="https://doc.rust-lang.org/favicon.ico"></head><body class="rustdoc fn"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu">&#9776;</div><a href='../../core/index.html'><img src='https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png' alt='logo' width='100'></a><div class="sidebar-elems"><p class='location'><a href='../index.html'>core</a>::<wbr><a href='index.html'>iter</a></p><script>window.sidebarCurrent = {name: 'repeat_with', ty: 'fn', relpath: ''};</script><script defer src="sidebar-items.js"></script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!"><img src="../../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices"></div></div><script src="../../theme.js"></script><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"><a id="settings-menu" href="../../settings.html"><img src="../../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><span class='in-band'>Function <a href='../index.html'>core</a>::<wbr><a href='index.html'>iter</a>::<wbr><a class="fn" href=''>repeat_with</a></span><span class='out-of-band'><span class='since' title='Stable since Rust version 1.28.0'>1.28.0</span><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 class='srclink' href='../../src/core/iter/sources.rs.html#201-203' title='goto source code'>[src]</a></span></h1><div class="important-traits"><div class='tooltip'>ⓘ<span class='tooltiptext'>Important traits for <a class="struct" href="../../core/iter/struct.RepeatWith.html" title="struct core::iter::RepeatWith">RepeatWith</a>&lt;F&gt;</span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../core/iter/struct.RepeatWith.html" title="struct core::iter::RepeatWith">RepeatWith</a>&lt;F&gt;</h3><code class="content"><span class="where fmt-newline">impl&lt;A, F:&nbsp;<a class="trait" href="../../core/ops/trait.FnMut.html" title="trait core::ops::FnMut">FnMut</a>() -&gt; A&gt; <a class="trait" href="../../core/iter/trait.Iterator.html" title="trait core::iter::Iterator">Iterator</a> for <a class="struct" href="../../core/iter/struct.RepeatWith.html" title="struct core::iter::RepeatWith">RepeatWith</a>&lt;F&gt;</span><span class="where fmt-newline">    type <a href='../../core/iter/trait.Iterator.html#associatedtype.Item' class="type">Item</a> = A;</span></code></div></div><pre class='rust fn'>pub fn repeat_with&lt;A, F:&nbsp;<a class="trait" href="../../core/ops/trait.FnMut.html" title="trait core::ops::FnMut">FnMut</a>() -&gt; A&gt;(repeater: F) -&gt; <a class="struct" href="../../core/iter/struct.RepeatWith.html" title="struct core::iter::RepeatWith">RepeatWith</a>&lt;F&gt;</pre><div class='docblock'><p>Creates a new iterator that repeats elements of type <code>A</code> endlessly by
applying the provided closure, the repeater, <code>F: FnMut() -&gt; A</code>.</p>
<p>The <code>repeat_with()</code> function calls the repeater over and over and over and
over and over and 🔁.</p>
<p>Infinite iterators like <code>repeat_with()</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>
<p>If the element type of the iterator you need implements <code>Clone</code>, and
it is OK to keep the source element in memory, you should instead use
the <a href="fn.repeat.html"><code>repeat</code></a> function.</p>
<p>An iterator produced by <code>repeat_with()</code> is not a <code>DoubleEndedIterator</code>.
If you need <code>repeat_with()</code> to return a <code>DoubleEndedIterator</code>,
please open a GitHub issue explaining your use case.</p>
<h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1>
<p>Basic usage:</p>

<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">iter</span>;

<span class="comment">// let&#39;s assume we have some value of a type that is not `Clone`</span>
<span class="comment">// or which don&#39;t want to have in memory just yet because it is expensive:</span>
<span class="attribute">#[<span class="ident">derive</span>(<span class="ident">PartialEq</span>, <span class="ident">Debug</span>)]</span>
<span class="kw">struct</span> <span class="ident">Expensive</span>;

<span class="comment">// a particular value forever:</span>
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">things</span> <span class="op">=</span> <span class="ident">iter</span>::<span class="ident">repeat_with</span>(<span class="op">||</span> <span class="ident">Expensive</span>);

<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="ident">Expensive</span>), <span class="ident">things</span>.<span class="ident">next</span>());
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="ident">Expensive</span>), <span class="ident">things</span>.<span class="ident">next</span>());
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="ident">Expensive</span>), <span class="ident">things</span>.<span class="ident">next</span>());
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="ident">Expensive</span>), <span class="ident">things</span>.<span class="ident">next</span>());
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="prelude-val">Some</span>(<span class="ident">Expensive</span>), <span class="ident">things</span>.<span class="ident">next</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Aiter%3B%0A%0A%2F%2F%20let's%20assume%20we%20have%20some%20value%20of%20a%20type%20that%20is%20not%20%60Clone%60%0A%2F%2F%20or%20which%20don't%20want%20to%20have%20in%20memory%20just%20yet%20because%20it%20is%20expensive%3A%0A%23%5Bderive(PartialEq%2C%20Debug)%5D%0Astruct%20Expensive%3B%0A%0A%2F%2F%20a%20particular%20value%20forever%3A%0Alet%20mut%20things%20%3D%20iter%3A%3Arepeat_with(%7C%7C%20Expensive)%3B%0A%0Aassert_eq!(Some(Expensive)%2C%20things.next())%3B%0Aassert_eq!(Some(Expensive)%2C%20things.next())%3B%0Aassert_eq!(Some(Expensive)%2C%20things.next())%3B%0Aassert_eq!(Some(Expensive)%2C%20things.next())%3B%0Aassert_eq!(Some(Expensive)%2C%20things.next())%3B%0A%7D">Run</a></pre>
<p>Using mutation and going finite:</p>

<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">iter</span>;

<span class="comment">// From the zeroth to the third power of two:</span>
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">curr</span> <span class="op">=</span> <span class="number">1</span>;
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">pow2</span> <span class="op">=</span> <span class="ident">iter</span>::<span class="ident">repeat_with</span>(<span class="op">||</span> { <span class="kw">let</span> <span class="ident">tmp</span> <span class="op">=</span> <span class="ident">curr</span>; <span class="ident">curr</span> <span class="op">*=</span> <span class="number">2</span>; <span class="ident">tmp</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">1</span>), <span class="ident">pow2</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">2</span>), <span class="ident">pow2</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">pow2</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">8</span>), <span class="ident">pow2</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">pow2</span>.<span class="ident">next</span>());<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Aiter%3B%0A%0A%2F%2F%20From%20the%20zeroth%20to%20the%20third%20power%20of%20two%3A%0Alet%20mut%20curr%20%3D%201%3B%0Alet%20mut%20pow2%20%3D%20iter%3A%3Arepeat_with(%7C%7C%20%7B%20let%20tmp%20%3D%20curr%3B%20curr%20*%3D%202%3B%20tmp%20%7D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.take(4)%3B%0A%0Aassert_eq!(Some(1)%2C%20pow2.next())%3B%0Aassert_eq!(Some(2)%2C%20pow2.next())%3B%0Aassert_eq!(Some(4)%2C%20pow2.next())%3B%0Aassert_eq!(Some(8)%2C%20pow2.next())%3B%0A%0A%2F%2F%20...%20and%20now%20we're%20done%0Aassert_eq!(None%2C%20pow2.next())%3B%0A%7D">Run</a></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><kbd>?</kbd></dt><dd>Show this help dialog</dd><dt><kbd>S</kbd></dt><dd>Focus the search field</dd><dt><kbd>↑</kbd></dt><dd>Move up in search results</dd><dt><kbd>↓</kbd></dt><dd>Move down in search results</dd><dt><kbd>↹</kbd></dt><dd>Switch tab</dd><dt><kbd>&#9166;</kbd></dt><dd>Go to active search result</dd><dt><kbd>+</kbd></dt><dd>Expand all sections</dd><dt><kbd>-</kbd></dt><dd>Collapse all sections</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> or <code>* -> vec</code>)</p><p>Search multiple things at once by splitting your query with comma (e.g. <code>str,u8</code> or <code>String,struct:Vec,test</code>)</p></div></div></aside><script>window.rootPath = "../../";window.currentCrate = "core";</script><script src="../../aliases.js"></script><script src="../../main.js"></script><script defer src="../../search-index.js"></script></body></html>