fn.unfold.html 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta name="generator" content="rustdoc">
  7. <meta name="description" content="API documentation for the Rust `unfold` fn in crate `futures`.">
  8. <meta name="keywords" content="rust, rustlang, rust-lang, unfold">
  9. <title>futures::stream::unfold - Rust</title>
  10. <link rel="stylesheet" type="text/css" href="../../normalize.css">
  11. <link rel="stylesheet" type="text/css" href="../../rustdoc.css" id="mainThemeStyle">
  12. <link rel="stylesheet" type="text/css" href="../../dark.css">
  13. <link rel="stylesheet" type="text/css" href="../../main.css" id="themeStyle">
  14. <script src="../../storage.js"></script>
  15. </head>
  16. <body class="rustdoc fn">
  17. <!--[if lte IE 8]>
  18. <div class="warning">
  19. This old browser is unsupported and will most likely display funky
  20. things.
  21. </div>
  22. <![endif]-->
  23. <nav class="sidebar">
  24. <div class="sidebar-menu">&#9776;</div>
  25. <p class='location'><a href='../index.html'>futures</a>::<wbr><a href='index.html'>stream</a></p><script>window.sidebarCurrent = {name: 'unfold', ty: 'fn', relpath: ''};</script><script defer src="sidebar-items.js"></script>
  26. </nav>
  27. <div class="theme-picker">
  28. <button id="theme-picker" aria-label="Pick another theme!">
  29. <img src="../../brush.svg" width="18" alt="Pick another theme!">
  30. </button>
  31. <div id="theme-choices"></div>
  32. </div>
  33. <script src="../../theme.js"></script>
  34. <nav class="sub">
  35. <form class="search-form js-only">
  36. <div class="search-container">
  37. <input class="search-input" name="search"
  38. autocomplete="off"
  39. placeholder="Click or press ‘S’ to search, ‘?’ for more options…"
  40. type="search">
  41. </div>
  42. </form>
  43. </nav>
  44. <section id='main' class="content">
  45. <h1 class='fqn'><span class='in-band'>Function <a href='../index.html'>futures</a>::<wbr><a href='index.html'>stream</a>::<wbr><a class="fn" href=''>unfold</a></span><span class='out-of-band'><span id='render-detail'>
  46. <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">
  47. [<span class='inner'>&#x2212;</span>]
  48. </a>
  49. </span><a class='srclink' href='../../src/futures/stream/unfold.rs.html#49-57' title='goto source code'>[src]</a></span></h1>
  50. <pre class='rust fn'>pub fn unfold&lt;T, F, Fut, It&gt;(init: T, f: F) -&gt; <a class="struct" href="../../futures/stream/struct.Unfold.html" title="struct futures::stream::Unfold">Unfold</a>&lt;T, F, Fut&gt; <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.FnMut.html" title="trait core::ops::function::FnMut">FnMut</a>(T) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;Fut&gt;,<br>&nbsp;&nbsp;&nbsp;&nbsp;Fut: <a class="trait" href="../../futures/future/trait.IntoFuture.html" title="trait futures::future::IntoFuture">IntoFuture</a>&lt;Item = <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.tuple.html">(</a>It, T<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.tuple.html">)</a>&gt;,&nbsp;</span></pre><div class='docblock'><p>Creates a <code>Stream</code> from a seed and a closure returning a <code>Future</code>.</p>
  51. <p>This function is the dual for the <code>Stream::fold()</code> adapter: while
  52. <code>Stream::fold()</code> reduces a <code>Stream</code> to one single value, <code>unfold()</code> creates a
  53. <code>Stream</code> from a seed value.</p>
  54. <p><code>unfold()</code> will call the provided closure with the provided seed, then wait
  55. for the returned <code>Future</code> to complete with <code>(a, b)</code>. It will then yield the
  56. value <code>a</code>, and use <code>b</code> as the next internal state.</p>
  57. <p>If the closure returns <code>None</code> instead of <code>Some(Future)</code>, then the <code>unfold()</code>
  58. will stop producing items and return <code>Ok(Async::Ready(None))</code> in future
  59. calls to <code>poll()</code>.</p>
  60. <p>In case of error generated by the returned <code>Future</code>, the error will be
  61. returned by the <code>Stream</code>. The <code>Stream</code> will then yield
  62. <code>Ok(Async::Ready(None))</code> in future calls to <code>poll()</code>.</p>
  63. <p>This function can typically be used when wanting to go from the &quot;world of
  64. futures&quot; to the &quot;world of streams&quot;: the provided closure can build a
  65. <code>Future</code> using other library functions working on futures, and <code>unfold()</code>
  66. will turn it into a <code>Stream</code> by repeating the operation.</p>
  67. <h1 id="example" class="section-header"><a href="#example">Example</a></h1>
  68. <pre class="rust rust-example-rendered">
  69. <span class="kw">use</span> <span class="ident">futures</span>::<span class="ident">stream</span>::{<span class="self">self</span>, <span class="ident">Stream</span>};
  70. <span class="kw">use</span> <span class="ident">futures</span>::<span class="ident">future</span>::{<span class="self">self</span>, <span class="ident">Future</span>};
  71. <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">stream</span> <span class="op">=</span> <span class="ident">stream</span>::<span class="ident">unfold</span>(<span class="number">0</span>, <span class="op">|</span><span class="ident">state</span><span class="op">|</span> {
  72. <span class="kw">if</span> <span class="ident">state</span> <span class="op">&lt;=</span> <span class="number">2</span> {
  73. <span class="kw">let</span> <span class="ident">next_state</span> <span class="op">=</span> <span class="ident">state</span> <span class="op">+</span> <span class="number">1</span>;
  74. <span class="kw">let</span> <span class="ident">yielded</span> <span class="op">=</span> <span class="ident">state</span> <span class="op">*</span> <span class="number">2</span>;
  75. <span class="kw">let</span> <span class="ident">fut</span> <span class="op">=</span> <span class="ident">future</span>::<span class="ident">ok</span>::<span class="op">&lt;</span>_, <span class="ident">u32</span><span class="op">&gt;</span>((<span class="ident">yielded</span>, <span class="ident">next_state</span>));
  76. <span class="prelude-val">Some</span>(<span class="ident">fut</span>)
  77. } <span class="kw">else</span> {
  78. <span class="prelude-val">None</span>
  79. }
  80. });
  81. <span class="kw">let</span> <span class="ident">result</span> <span class="op">=</span> <span class="ident">stream</span>.<span class="ident">collect</span>().<span class="ident">wait</span>();
  82. <span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">result</span>, <span class="prelude-val">Ok</span>(<span class="macro">vec</span><span class="macro">!</span>[<span class="number">0</span>, <span class="number">2</span>, <span class="number">4</span>]));</pre>
  83. </div></section>
  84. <section id='search' class="content hidden"></section>
  85. <section class="footer"></section>
  86. <aside id="help" class="hidden">
  87. <div>
  88. <h1 class="hidden">Help</h1>
  89. <div class="shortcuts">
  90. <h2>Keyboard Shortcuts</h2>
  91. <dl>
  92. <dt><kbd>?</kbd></dt>
  93. <dd>Show this help dialog</dd>
  94. <dt><kbd>S</kbd></dt>
  95. <dd>Focus the search field</dd>
  96. <dt><kbd>↑</kbd></dt>
  97. <dd>Move up in search results</dd>
  98. <dt><kbd>↓</kbd></dt>
  99. <dd>Move down in search results</dd>
  100. <dt><kbd>↹</kbd></dt>
  101. <dd>Switch tab</dd>
  102. <dt><kbd>&#9166;</kbd></dt>
  103. <dd>Go to active search result</dd>
  104. <dt><kbd>+</kbd></dt>
  105. <dd>Expand all sections</dd>
  106. <dt><kbd>-</kbd></dt>
  107. <dd>Collapse all sections</dd>
  108. </dl>
  109. </div>
  110. <div class="infos">
  111. <h2>Search Tricks</h2>
  112. <p>
  113. Prefix searches with a type followed by a colon (e.g.
  114. <code>fn:</code>) to restrict the search to a given type.
  115. </p>
  116. <p>
  117. Accepted types are: <code>fn</code>, <code>mod</code>,
  118. <code>struct</code>, <code>enum</code>,
  119. <code>trait</code>, <code>type</code>, <code>macro</code>,
  120. and <code>const</code>.
  121. </p>
  122. <p>
  123. Search functions by type signature (e.g.
  124. <code>vec -> usize</code> or <code>* -> vec</code>)
  125. </p>
  126. </div>
  127. </div>
  128. </aside>
  129. <script>
  130. window.rootPath = "../../";
  131. window.currentCrate = "futures";
  132. </script>
  133. <script src="../../main.js"></script>
  134. <script defer src="../../search-index.js"></script>
  135. </body>
  136. </html>