| <!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="An extension trait for `Stream`s that provides a variety of convenient combinator functions."><meta name="keywords" content="rust, rustlang, rust-lang, StreamExt"><title>StreamExt in futures_util::stream - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../../normalize.css"><link rel="stylesheet" href="../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../../ayu.css" disabled><link rel="stylesheet" href="../../dark.css" disabled><link rel="stylesheet" href="../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../storage.js"></script><script defer src="sidebar-items.js"></script><script defer src="../../main.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../favicon.svg"></head><body class="rustdoc trait"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../../futures_util/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a><h2></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../../futures_util/index.html"><div class="logo-container"><img class="rust-logo" src="../../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">StreamExt</a></h2><div class="sidebar-elems"><section><h3><a href="#provided-methods">Provided Methods</a></h3><ul class="block"><li><a href="#method.all">all</a></li><li><a href="#method.any">any</a></li><li><a href="#method.boxed">boxed</a></li><li><a href="#method.boxed_local">boxed_local</a></li><li><a href="#method.buffer_unordered">buffer_unordered</a></li><li><a href="#method.buffered">buffered</a></li><li><a href="#method.by_ref">by_ref</a></li><li><a href="#method.catch_unwind">catch_unwind</a></li><li><a href="#method.chain">chain</a></li><li><a href="#method.chunks">chunks</a></li><li><a href="#method.collect">collect</a></li><li><a href="#method.concat">concat</a></li><li><a href="#method.count">count</a></li><li><a href="#method.cycle">cycle</a></li><li><a href="#method.enumerate">enumerate</a></li><li><a href="#method.filter">filter</a></li><li><a href="#method.filter_map">filter_map</a></li><li><a href="#method.flat_map">flat_map</a></li><li><a href="#method.flat_map_unordered">flat_map_unordered</a></li><li><a href="#method.flatten">flatten</a></li><li><a href="#method.flatten_unordered">flatten_unordered</a></li><li><a href="#method.fold">fold</a></li><li><a href="#method.for_each">for_each</a></li><li><a href="#method.for_each_concurrent">for_each_concurrent</a></li><li><a href="#method.forward">forward</a></li><li><a href="#method.fuse">fuse</a></li><li><a href="#method.inspect">inspect</a></li><li><a href="#method.into_future">into_future</a></li><li><a href="#method.left_stream">left_stream</a></li><li><a href="#method.map">map</a></li><li><a href="#method.next">next</a></li><li><a href="#method.peekable">peekable</a></li><li><a href="#method.poll_next_unpin">poll_next_unpin</a></li><li><a href="#method.ready_chunks">ready_chunks</a></li><li><a href="#method.right_stream">right_stream</a></li><li><a href="#method.scan">scan</a></li><li><a href="#method.select_next_some">select_next_some</a></li><li><a href="#method.skip">skip</a></li><li><a href="#method.skip_while">skip_while</a></li><li><a href="#method.split">split</a></li><li><a href="#method.take">take</a></li><li><a href="#method.take_until">take_until</a></li><li><a href="#method.take_while">take_while</a></li><li><a href="#method.then">then</a></li><li><a href="#method.unzip">unzip</a></li><li><a href="#method.zip">zip</a></li></ul><h3><a href="#implementors">Implementors</a></h3></section><h2><a href="index.html">In futures_util::stream</a></h2></div></nav><main><div class="width-limiter"><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../wheel.svg"></a></div></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn">Trait <a href="../index.html">futures_util</a>::<wbr><a href="index.html">stream</a>::<wbr><a class="trait" href="#">StreamExt</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../../clipboard.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="srclink" href="../../src/futures_util/stream/stream/mod.rs.html#249-1691">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">−</span>]</a></span></div><div class="item-decl"><pre class="rust trait"><code>pub trait StreamExt: <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a> { |
| <details class="rustdoc-toggle type-contents-toggle"><summary class="hideme"><span>Show 46 methods</span></summary> fn <a href="#method.next" class="fnname">next</a>(&mut self) -> <a class="struct" href="struct.Next.html" title="struct futures_util::stream::Next">Next</a><'_, Self><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="struct.Next.html" title="struct futures_util::stream::Next">Next</a><'_, St></span><code class="content"><span class="where fmt-newline">impl<St: ?Sized + <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a> + Unpin> Future for <a class="struct" href="struct.Next.html" title="struct futures_util::stream::Next">Next</a><'_, St></span><span class="where fmt-newline"> type <a class="associatedtype">Output</a> = Option<St::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>>;</span></code></span></span></span></span><br> <span class="where">where<br> Self: Unpin</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.into_future" class="fnname">into_future</a>(self) -> <a class="struct" href="struct.StreamFuture.html" title="struct futures_util::stream::StreamFuture">StreamFuture</a><Self><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="struct.StreamFuture.html" title="struct futures_util::stream::StreamFuture">StreamFuture</a><St></span><code class="content"><span class="where fmt-newline">impl<St: <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a> + Unpin> Future for <a class="struct" href="struct.StreamFuture.html" title="struct futures_util::stream::StreamFuture">StreamFuture</a><St></span><span class="where fmt-newline"> type <a class="associatedtype">Output</a> = (Option<St::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>>, St);</span></code></span></span></span></span><br> <span class="where">where<br> Self: Sized + Unpin</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.map" class="fnname">map</a><T, F>(self, f: F) -> <a class="struct" href="struct.Map.html" title="struct futures_util::stream::Map">Map</a><Self, F><br> <span class="where">where<br> F: FnMut(Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>) -> T,<br> Self: Sized</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.enumerate" class="fnname">enumerate</a>(self) -> <a class="struct" href="struct.Enumerate.html" title="struct futures_util::stream::Enumerate">Enumerate</a><Self><br> <span class="where">where<br> Self: Sized</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.filter" class="fnname">filter</a><Fut, F>(self, f: F) -> <a class="struct" href="struct.Filter.html" title="struct futures_util::stream::Filter">Filter</a><Self, Fut, F><br> <span class="where">where<br> F: FnMut(&Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>) -> Fut,<br> Fut: Future<Output = bool>,<br> Self: Sized</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.filter_map" class="fnname">filter_map</a><Fut, T, F>(self, f: F) -> <a class="struct" href="struct.FilterMap.html" title="struct futures_util::stream::FilterMap">FilterMap</a><Self, Fut, F><br> <span class="where">where<br> F: FnMut(Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>) -> Fut,<br> Fut: Future<Output = Option<T>>,<br> Self: Sized</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.then" class="fnname">then</a><Fut, F>(self, f: F) -> <a class="struct" href="struct.Then.html" title="struct futures_util::stream::Then">Then</a><Self, Fut, F><br> <span class="where">where<br> F: FnMut(Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>) -> Fut,<br> Fut: Future,<br> Self: Sized</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.collect" class="fnname">collect</a><C: Default + Extend<Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>>>(self) -> <a class="struct" href="struct.Collect.html" title="struct futures_util::stream::Collect">Collect</a><Self, C><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="struct.Collect.html" title="struct futures_util::stream::Collect">Collect</a><St, C></span><code class="content"><span class="where fmt-newline">impl<St, C> Future for <a class="struct" href="struct.Collect.html" title="struct futures_util::stream::Collect">Collect</a><St, C><span class="where fmt-newline">where<br> St: <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a>,<br> C: Default + Extend<St::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>>,</span></span><span class="where fmt-newline"> type <a class="associatedtype">Output</a> = C;</span></code></span></span></span></span><br> <span class="where">where<br> Self: Sized</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.unzip" class="fnname">unzip</a><A, B, FromA, FromB>(self) -> <a class="struct" href="struct.Unzip.html" title="struct futures_util::stream::Unzip">Unzip</a><Self, FromA, FromB><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="struct.Unzip.html" title="struct futures_util::stream::Unzip">Unzip</a><St, FromA, FromB></span><code class="content"><span class="where fmt-newline">impl<St, A, B, FromA, FromB> Future for <a class="struct" href="struct.Unzip.html" title="struct futures_util::stream::Unzip">Unzip</a><St, FromA, FromB><span class="where fmt-newline">where<br> St: <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a><Item = (A, B)>,<br> FromA: Default + Extend<A>,<br> FromB: Default + Extend<B>,</span></span><span class="where fmt-newline"> type <a class="associatedtype">Output</a> = (FromA, FromB);</span></code></span></span></span></span><br> <span class="where">where<br> FromA: Default + Extend<A>,<br> FromB: Default + Extend<B>,<br> Self: Sized + <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a><Item = (A, B)></span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.concat" class="fnname">concat</a>(self) -> <a class="struct" href="struct.Concat.html" title="struct futures_util::stream::Concat">Concat</a><Self><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="struct.Concat.html" title="struct futures_util::stream::Concat">Concat</a><St></span><code class="content"><span class="where fmt-newline">impl<St> Future for <a class="struct" href="struct.Concat.html" title="struct futures_util::stream::Concat">Concat</a><St><span class="where fmt-newline">where<br> St: <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a>,<br> St::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>: Extend<<St::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a> as IntoIterator>::Item> + IntoIterator + Default,</span></span><span class="where fmt-newline"> type <a class="associatedtype">Output</a> = St::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>;</span></code></span></span></span></span><br> <span class="where">where<br> Self: Sized,<br> Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>: Extend<<<Self as <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a>>::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a> as IntoIterator>::Item> + IntoIterator + Default</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.count" class="fnname">count</a>(self) -> <a class="struct" href="struct.Count.html" title="struct futures_util::stream::Count">Count</a><Self><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="struct.Count.html" title="struct futures_util::stream::Count">Count</a><St></span><code class="content"><span class="where fmt-newline">impl<St: <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a>> Future for <a class="struct" href="struct.Count.html" title="struct futures_util::stream::Count">Count</a><St></span><span class="where fmt-newline"> type <a class="associatedtype">Output</a> = usize;</span></code></span></span></span></span><br> <span class="where">where<br> Self: Sized</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.cycle" class="fnname">cycle</a>(self) -> <a class="struct" href="struct.Cycle.html" title="struct futures_util::stream::Cycle">Cycle</a><Self><br> <span class="where">where<br> Self: Sized + Clone</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.fold" class="fnname">fold</a><T, Fut, F>(self, init: T, f: F) -> <a class="struct" href="struct.Fold.html" title="struct futures_util::stream::Fold">Fold</a><Self, Fut, T, F><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="struct.Fold.html" title="struct futures_util::stream::Fold">Fold</a><St, Fut, T, F></span><code class="content"><span class="where fmt-newline">impl<St, Fut, T, F> Future for <a class="struct" href="struct.Fold.html" title="struct futures_util::stream::Fold">Fold</a><St, Fut, T, F><span class="where fmt-newline">where<br> St: <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a>,<br> F: FnMut(T, St::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>) -> Fut,<br> Fut: Future<Output = T>,</span></span><span class="where fmt-newline"> type <a class="associatedtype">Output</a> = T;</span></code></span></span></span></span><br> <span class="where">where<br> F: FnMut(T, Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>) -> Fut,<br> Fut: Future<Output = T>,<br> Self: Sized</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.any" class="fnname">any</a><Fut, F>(self, f: F) -> <a class="struct" href="struct.Any.html" title="struct futures_util::stream::Any">Any</a><Self, Fut, F><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="struct.Any.html" title="struct futures_util::stream::Any">Any</a><St, Fut, F></span><code class="content"><span class="where fmt-newline">impl<St, Fut, F> Future for <a class="struct" href="struct.Any.html" title="struct futures_util::stream::Any">Any</a><St, Fut, F><span class="where fmt-newline">where<br> St: <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a>,<br> F: FnMut(St::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>) -> Fut,<br> Fut: Future<Output = bool>,</span></span><span class="where fmt-newline"> type <a class="associatedtype">Output</a> = bool;</span></code></span></span></span></span><br> <span class="where">where<br> F: FnMut(Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>) -> Fut,<br> Fut: Future<Output = bool>,<br> Self: Sized</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.all" class="fnname">all</a><Fut, F>(self, f: F) -> <a class="struct" href="struct.All.html" title="struct futures_util::stream::All">All</a><Self, Fut, F><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="struct.All.html" title="struct futures_util::stream::All">All</a><St, Fut, F></span><code class="content"><span class="where fmt-newline">impl<St, Fut, F> Future for <a class="struct" href="struct.All.html" title="struct futures_util::stream::All">All</a><St, Fut, F><span class="where fmt-newline">where<br> St: <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a>,<br> F: FnMut(St::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>) -> Fut,<br> Fut: Future<Output = bool>,</span></span><span class="where fmt-newline"> type <a class="associatedtype">Output</a> = bool;</span></code></span></span></span></span><br> <span class="where">where<br> F: FnMut(Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>) -> Fut,<br> Fut: Future<Output = bool>,<br> Self: Sized</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.flatten" class="fnname">flatten</a>(self) -> <a class="struct" href="struct.Flatten.html" title="struct futures_util::stream::Flatten">Flatten</a><Self><br> <span class="where">where<br> Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>: <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a>,<br> Self: Sized</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.flatten_unordered" class="fnname">flatten_unordered</a>(<br> self,<br> limit: impl Into<Option<usize>><br> ) -> <a class="type" href="type.FlattenUnordered.html" title="type futures_util::stream::FlattenUnordered">FlattenUnordered</a><Self><br> <span class="where">where<br> Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>: <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a> + Unpin,<br> Self: Sized</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.flat_map" class="fnname">flat_map</a><U, F>(self, f: F) -> <a class="struct" href="struct.FlatMap.html" title="struct futures_util::stream::FlatMap">FlatMap</a><Self, U, F><br> <span class="where">where<br> F: FnMut(Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>) -> U,<br> U: <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a>,<br> Self: Sized</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.flat_map_unordered" class="fnname">flat_map_unordered</a><U, F>(<br> self,<br> limit: impl Into<Option<usize>>,<br> f: F<br> ) -> <a class="struct" href="struct.FlatMapUnordered.html" title="struct futures_util::stream::FlatMapUnordered">FlatMapUnordered</a><Self, U, F><br> <span class="where">where<br> U: <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a> + Unpin,<br> F: FnMut(Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>) -> U,<br> Self: Sized</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.scan" class="fnname">scan</a><S, B, Fut, F>(self, initial_state: S, f: F) -> <a class="struct" href="struct.Scan.html" title="struct futures_util::stream::Scan">Scan</a><Self, S, Fut, F><br> <span class="where">where<br> F: FnMut(&mut S, Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>) -> Fut,<br> Fut: Future<Output = Option<B>>,<br> Self: Sized</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.skip_while" class="fnname">skip_while</a><Fut, F>(self, f: F) -> <a class="struct" href="struct.SkipWhile.html" title="struct futures_util::stream::SkipWhile">SkipWhile</a><Self, Fut, F><br> <span class="where">where<br> F: FnMut(&Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>) -> Fut,<br> Fut: Future<Output = bool>,<br> Self: Sized</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.take_while" class="fnname">take_while</a><Fut, F>(self, f: F) -> <a class="struct" href="struct.TakeWhile.html" title="struct futures_util::stream::TakeWhile">TakeWhile</a><Self, Fut, F><br> <span class="where">where<br> F: FnMut(&Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>) -> Fut,<br> Fut: Future<Output = bool>,<br> Self: Sized</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.take_until" class="fnname">take_until</a><Fut>(self, fut: Fut) -> <a class="struct" href="struct.TakeUntil.html" title="struct futures_util::stream::TakeUntil">TakeUntil</a><Self, Fut><br> <span class="where">where<br> Fut: Future,<br> Self: Sized</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.for_each" class="fnname">for_each</a><Fut, F>(self, f: F) -> <a class="struct" href="struct.ForEach.html" title="struct futures_util::stream::ForEach">ForEach</a><Self, Fut, F><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="struct.ForEach.html" title="struct futures_util::stream::ForEach">ForEach</a><St, Fut, F></span><code class="content"><span class="where fmt-newline">impl<St, Fut, F> Future for <a class="struct" href="struct.ForEach.html" title="struct futures_util::stream::ForEach">ForEach</a><St, Fut, F><span class="where fmt-newline">where<br> St: <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a>,<br> F: FnMut(St::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>) -> Fut,<br> Fut: Future<Output = ()>,</span></span><span class="where fmt-newline"> type <a class="associatedtype">Output</a> = ();</span></code></span></span></span></span><br> <span class="where">where<br> F: FnMut(Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>) -> Fut,<br> Fut: Future<Output = ()>,<br> Self: Sized</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.for_each_concurrent" class="fnname">for_each_concurrent</a><Fut, F>(<br> self,<br> limit: impl Into<Option<usize>>,<br> f: F<br> ) -> <a class="struct" href="struct.ForEachConcurrent.html" title="struct futures_util::stream::ForEachConcurrent">ForEachConcurrent</a><Self, Fut, F><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="struct.ForEachConcurrent.html" title="struct futures_util::stream::ForEachConcurrent">ForEachConcurrent</a><St, Fut, F></span><code class="content"><span class="where fmt-newline">impl<St, Fut, F> Future for <a class="struct" href="struct.ForEachConcurrent.html" title="struct futures_util::stream::ForEachConcurrent">ForEachConcurrent</a><St, Fut, F><span class="where fmt-newline">where<br> St: <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a>,<br> F: FnMut(St::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>) -> Fut,<br> Fut: Future<Output = ()>,</span></span><span class="where fmt-newline"> type <a class="associatedtype">Output</a> = ();</span></code></span></span></span></span><br> <span class="where">where<br> F: FnMut(Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>) -> Fut,<br> Fut: Future<Output = ()>,<br> Self: Sized</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.take" class="fnname">take</a>(self, n: usize) -> <a class="struct" href="struct.Take.html" title="struct futures_util::stream::Take">Take</a><Self><br> <span class="where">where<br> Self: Sized</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.skip" class="fnname">skip</a>(self, n: usize) -> <a class="struct" href="struct.Skip.html" title="struct futures_util::stream::Skip">Skip</a><Self><br> <span class="where">where<br> Self: Sized</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.fuse" class="fnname">fuse</a>(self) -> <a class="struct" href="struct.Fuse.html" title="struct futures_util::stream::Fuse">Fuse</a><Self><br> <span class="where">where<br> Self: Sized</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.by_ref" class="fnname">by_ref</a>(&mut self) -> &mut Self { ... } |
| <span class="item-spacer"></span> fn <a href="#method.catch_unwind" class="fnname">catch_unwind</a>(self) -> <a class="struct" href="struct.CatchUnwind.html" title="struct futures_util::stream::CatchUnwind">CatchUnwind</a><Self><br> <span class="where">where<br> Self: Sized + UnwindSafe</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.boxed" class="fnname">boxed</a><'a>(self) -> <a class="type" href="type.BoxStream.html" title="type futures_util::stream::BoxStream">BoxStream</a><'a, Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>><br> <span class="where">where<br> Self: Sized + Send + 'a</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.boxed_local" class="fnname">boxed_local</a><'a>(self) -> <a class="type" href="type.LocalBoxStream.html" title="type futures_util::stream::LocalBoxStream">LocalBoxStream</a><'a, Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>><br> <span class="where">where<br> Self: Sized + 'a</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.buffered" class="fnname">buffered</a>(self, n: usize) -> <a class="struct" href="struct.Buffered.html" title="struct futures_util::stream::Buffered">Buffered</a><Self><br> <span class="where">where<br> Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>: Future,<br> Self: Sized</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.buffer_unordered" class="fnname">buffer_unordered</a>(self, n: usize) -> <a class="struct" href="struct.BufferUnordered.html" title="struct futures_util::stream::BufferUnordered">BufferUnordered</a><Self><br> <span class="where">where<br> Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>: Future,<br> Self: Sized</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.zip" class="fnname">zip</a><St>(self, other: St) -> <a class="struct" href="struct.Zip.html" title="struct futures_util::stream::Zip">Zip</a><Self, St><br> <span class="where">where<br> St: <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a>,<br> Self: Sized</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.chain" class="fnname">chain</a><St>(self, other: St) -> <a class="struct" href="struct.Chain.html" title="struct futures_util::stream::Chain">Chain</a><Self, St><br> <span class="where">where<br> St: <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a><Item = Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>>,<br> Self: Sized</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.peekable" class="fnname">peekable</a>(self) -> <a class="struct" href="struct.Peekable.html" title="struct futures_util::stream::Peekable">Peekable</a><Self><br> <span class="where">where<br> Self: Sized</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.chunks" class="fnname">chunks</a>(self, capacity: usize) -> <a class="struct" href="struct.Chunks.html" title="struct futures_util::stream::Chunks">Chunks</a><Self><br> <span class="where">where<br> Self: Sized</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.ready_chunks" class="fnname">ready_chunks</a>(self, capacity: usize) -> <a class="struct" href="struct.ReadyChunks.html" title="struct futures_util::stream::ReadyChunks">ReadyChunks</a><Self><br> <span class="where">where<br> Self: Sized</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.forward" class="fnname">forward</a><S>(self, sink: S) -> <a class="struct" href="struct.Forward.html" title="struct futures_util::stream::Forward">Forward</a><Self, S><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="struct.Forward.html" title="struct futures_util::stream::Forward">Forward</a><St, Si></span><code class="content"><span class="where fmt-newline">impl<St, Si> Future for <a class="struct" href="struct.Forward.html" title="struct futures_util::stream::Forward">Forward</a><St, Si><span class="where fmt-newline">where<br> Forward<St, Si, St::<a class="associatedtype" href="trait.TryStream.html#associatedtype.Ok" title="type futures_util::stream::TryStream::Ok">Ok</a>>: Future,<br> St: <a class="trait" href="trait.TryStream.html" title="trait futures_util::stream::TryStream">TryStream</a>,</span></span><span class="where fmt-newline"> type <a class="associatedtype">Output</a> = <Forward<St, Si, St::<a class="associatedtype" href="trait.TryStream.html#associatedtype.Ok" title="type futures_util::stream::TryStream::Ok">Ok</a>> as Future>::Output;</span></code></span></span></span></span><br> <span class="where">where<br> S: <a class="trait" href="../sink/trait.Sink.html" title="trait futures_util::sink::Sink">Sink</a><Self::<a class="associatedtype" href="trait.TryStream.html#associatedtype.Ok" title="type futures_util::stream::TryStream::Ok">Ok</a>, Error = Self::<a class="associatedtype" href="trait.TryStream.html#associatedtype.Error" title="type futures_util::stream::TryStream::Error">Error</a>>,<br> Self: <a class="trait" href="trait.TryStream.html" title="trait futures_util::stream::TryStream">TryStream</a> + Sized</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.split" class="fnname">split</a><Item>(self) -> (<a class="struct" href="struct.SplitSink.html" title="struct futures_util::stream::SplitSink">SplitSink</a><Self, Item>, <a class="struct" href="struct.SplitStream.html" title="struct futures_util::stream::SplitStream">SplitStream</a><Self>)<br> <span class="where">where<br> Self: <a class="trait" href="../sink/trait.Sink.html" title="trait futures_util::sink::Sink">Sink</a><Item> + Sized</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.inspect" class="fnname">inspect</a><F>(self, f: F) -> <a class="struct" href="struct.Inspect.html" title="struct futures_util::stream::Inspect">Inspect</a><Self, F><br> <span class="where">where<br> F: FnMut(&Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>),<br> Self: Sized</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.left_stream" class="fnname">left_stream</a><B>(self) -> <a class="enum" href="../future/enum.Either.html" title="enum futures_util::future::Either">Either</a><Self, B><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="enum" href="../future/enum.Either.html" title="enum futures_util::future::Either">Either</a><A, B></span><code class="content"><span class="where fmt-newline">impl<A, B> Future for <a class="enum" href="../future/enum.Either.html" title="enum futures_util::future::Either">Either</a><A, B><span class="where fmt-newline">where<br> A: Future,<br> B: Future<Output = A::Output>,</span></span><span class="where fmt-newline"> type <a class="associatedtype">Output</a> = A::Output;</span></code></span></span></span></span><br> <span class="where">where<br> B: <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a><Item = Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>>,<br> Self: Sized</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.right_stream" class="fnname">right_stream</a><B>(self) -> <a class="enum" href="../future/enum.Either.html" title="enum futures_util::future::Either">Either</a><B, Self><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="enum" href="../future/enum.Either.html" title="enum futures_util::future::Either">Either</a><A, B></span><code class="content"><span class="where fmt-newline">impl<A, B> Future for <a class="enum" href="../future/enum.Either.html" title="enum futures_util::future::Either">Either</a><A, B><span class="where fmt-newline">where<br> A: Future,<br> B: Future<Output = A::Output>,</span></span><span class="where fmt-newline"> type <a class="associatedtype">Output</a> = A::Output;</span></code></span></span></span></span><br> <span class="where">where<br> B: <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a><Item = Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>>,<br> Self: Sized</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.poll_next_unpin" class="fnname">poll_next_unpin</a>(<br> &mut self,<br> cx: &mut Context<'_><br> ) -> Poll<Option<Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>>><br> <span class="where">where<br> Self: Unpin</span>, |
| { ... } |
| <span class="item-spacer"></span> fn <a href="#method.select_next_some" class="fnname">select_next_some</a>(&mut self) -> <a class="struct" href="struct.SelectNextSome.html" title="struct futures_util::stream::SelectNextSome">SelectNextSome</a><'_, Self><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="struct.SelectNextSome.html" title="struct futures_util::stream::SelectNextSome">SelectNextSome</a><'_, St></span><code class="content"><span class="where fmt-newline">impl<St: ?Sized + <a class="trait" href="trait.FusedStream.html" title="trait futures_util::stream::FusedStream">FusedStream</a> + Unpin> Future for <a class="struct" href="struct.SelectNextSome.html" title="struct futures_util::stream::SelectNextSome">SelectNextSome</a><'_, St></span><span class="where fmt-newline"> type <a class="associatedtype">Output</a> = St::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>;</span></code></span></span></span></span><br> <span class="where">where<br> Self: Unpin + <a class="trait" href="trait.FusedStream.html" title="trait futures_util::stream::FusedStream">FusedStream</a></span>, |
| { ... } |
| </details>}</code></pre></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>An extension trait for <code>Stream</code>s that provides a variety of convenient |
| combinator functions.</p> |
| </div></details><h2 id="provided-methods" class="small-section-header">Provided Methods<a href="#provided-methods" class="anchor"></a></h2><div class="methods"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.next" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#273-278">source</a><h4 class="code-header">fn <a href="#method.next" class="fnname">next</a>(&mut self) -> <a class="struct" href="struct.Next.html" title="struct futures_util::stream::Next">Next</a><'_, Self><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="struct.Next.html" title="struct futures_util::stream::Next">Next</a><'_, St></span><code class="content"><span class="where fmt-newline">impl<St: ?Sized + <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a> + Unpin> Future for <a class="struct" href="struct.Next.html" title="struct futures_util::stream::Next">Next</a><'_, St></span><span class="where fmt-newline"> type <a class="associatedtype">Output</a> = Option<St::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>>;</span></code></span></span></span></span><span class="where fmt-newline">where<br> Self: Unpin,</span></h4></section></summary><div class="docblock"><p>Creates a future that resolves to the next item in the stream.</p> |
| <p>Note that because <code>next</code> doesn’t take ownership over the stream, |
| the <a href="trait.Stream.html" title="Stream"><code>Stream</code></a> type must be [<code>Unpin</code>]. If you want to use <code>next</code> with a |
| <a href="Unpin"><code>!Unpin</code></a> stream, you’ll first have to pin the stream. This can |
| be done by boxing the stream using [<code>Box::pin</code>] or |
| pinning it to the stack using the <code>pin_mut!</code> macro from the <code>pin_utils</code> |
| crate.</p> |
| <h5 id="examples"><a href="#examples">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::stream::{<span class="self">self</span>, StreamExt}; |
| |
| <span class="kw">let </span><span class="kw-2">mut </span>stream = stream::iter(<span class="number">1</span>..=<span class="number">3</span>); |
| |
| <span class="macro">assert_eq!</span>(stream.next().<span class="kw">await</span>, <span class="prelude-val">Some</span>(<span class="number">1</span>)); |
| <span class="macro">assert_eq!</span>(stream.next().<span class="kw">await</span>, <span class="prelude-val">Some</span>(<span class="number">2</span>)); |
| <span class="macro">assert_eq!</span>(stream.next().<span class="kw">await</span>, <span class="prelude-val">Some</span>(<span class="number">3</span>)); |
| <span class="macro">assert_eq!</span>(stream.next().<span class="kw">await</span>, <span class="prelude-val">None</span>);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.into_future" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#308-313">source</a><h4 class="code-header">fn <a href="#method.into_future" class="fnname">into_future</a>(self) -> <a class="struct" href="struct.StreamFuture.html" title="struct futures_util::stream::StreamFuture">StreamFuture</a><Self><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="struct.StreamFuture.html" title="struct futures_util::stream::StreamFuture">StreamFuture</a><St></span><code class="content"><span class="where fmt-newline">impl<St: <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a> + Unpin> Future for <a class="struct" href="struct.StreamFuture.html" title="struct futures_util::stream::StreamFuture">StreamFuture</a><St></span><span class="where fmt-newline"> type <a class="associatedtype">Output</a> = (Option<St::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>>, St);</span></code></span></span></span></span><span class="where fmt-newline">where<br> Self: Sized + Unpin,</span></h4></section></summary><div class="docblock"><p>Converts this stream into a future of <code>(next_item, tail_of_stream)</code>. |
| If the stream terminates, then the next item is [<code>None</code>].</p> |
| <p>The returned future can be used to compose streams and futures together |
| by placing everything into the “world of futures”.</p> |
| <p>Note that because <code>into_future</code> moves the stream, the <a href="trait.Stream.html" title="Stream"><code>Stream</code></a> type |
| must be [<code>Unpin</code>]. If you want to use <code>into_future</code> with a |
| <a href="Unpin"><code>!Unpin</code></a> stream, you’ll first have to pin the stream. This can |
| be done by boxing the stream using [<code>Box::pin</code>] or |
| pinning it to the stack using the <code>pin_mut!</code> macro from the <code>pin_utils</code> |
| crate.</p> |
| <h5 id="examples-1"><a href="#examples-1">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::stream::{<span class="self">self</span>, StreamExt}; |
| |
| <span class="kw">let </span>stream = stream::iter(<span class="number">1</span>..=<span class="number">3</span>); |
| |
| <span class="kw">let </span>(item, stream) = stream.into_future().<span class="kw">await</span>; |
| <span class="macro">assert_eq!</span>(<span class="prelude-val">Some</span>(<span class="number">1</span>), item); |
| |
| <span class="kw">let </span>(item, stream) = stream.into_future().<span class="kw">await</span>; |
| <span class="macro">assert_eq!</span>(<span class="prelude-val">Some</span>(<span class="number">2</span>), item);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.map" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#338-344">source</a><h4 class="code-header">fn <a href="#method.map" class="fnname">map</a><T, F>(self, f: F) -> <a class="struct" href="struct.Map.html" title="struct futures_util::stream::Map">Map</a><Self, F><span class="where fmt-newline">where<br> F: FnMut(Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>) -> T,<br> Self: Sized,</span></h4></section></summary><div class="docblock"><p>Maps this stream’s items to a different type, returning a new stream of |
| the resulting type.</p> |
| <p>The provided closure is executed over all elements of this stream as |
| they are made available. It is executed inline with calls to |
| <a href="trait.Stream.html#tymethod.poll_next"><code>poll_next</code></a>.</p> |
| <p>Note that this function consumes the stream passed into it and returns a |
| wrapped version of it, similar to the existing <code>map</code> methods in the |
| standard library.</p> |
| <h5 id="examples-2"><a href="#examples-2">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::stream::{<span class="self">self</span>, StreamExt}; |
| |
| <span class="kw">let </span>stream = stream::iter(<span class="number">1</span>..=<span class="number">3</span>); |
| <span class="kw">let </span>stream = stream.map(|x| x + <span class="number">3</span>); |
| |
| <span class="macro">assert_eq!</span>(<span class="macro">vec!</span>[<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>], stream.collect::<Vec<<span class="kw">_</span>>>().<span class="kw">await</span>);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.enumerate" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#384-389">source</a><h4 class="code-header">fn <a href="#method.enumerate" class="fnname">enumerate</a>(self) -> <a class="struct" href="struct.Enumerate.html" title="struct futures_util::stream::Enumerate">Enumerate</a><Self><span class="where fmt-newline">where<br> Self: Sized,</span></h4></section></summary><div class="docblock"><p>Creates a stream which gives the current iteration count as well as |
| the next value.</p> |
| <p>The stream returned yields pairs <code>(i, val)</code>, where <code>i</code> is the |
| current index of iteration and <code>val</code> is the value returned by the |
| stream.</p> |
| <p><code>enumerate()</code> keeps its count as a [<code>usize</code>]. If you want to count by a |
| different sized integer, the <a href="trait.StreamExt.html#method.zip"><code>zip</code></a> function provides similar |
| functionality.</p> |
| <h5 id="overflow-behavior"><a href="#overflow-behavior">Overflow Behavior</a></h5> |
| <p>The method does no guarding against overflows, so enumerating more than |
| [<code>prim@usize::max_value()</code>] elements either produces the wrong result or panics. If |
| debug assertions are enabled, a panic is guaranteed.</p> |
| <h5 id="panics"><a href="#panics">Panics</a></h5> |
| <p>The returned stream might panic if the to-be-returned index would |
| overflow a [<code>usize</code>].</p> |
| <h5 id="examples-3"><a href="#examples-3">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::stream::{<span class="self">self</span>, StreamExt}; |
| |
| <span class="kw">let </span>stream = stream::iter(<span class="macro">vec!</span>[<span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'c'</span>]); |
| |
| <span class="kw">let </span><span class="kw-2">mut </span>stream = stream.enumerate(); |
| |
| <span class="macro">assert_eq!</span>(stream.next().<span class="kw">await</span>, <span class="prelude-val">Some</span>((<span class="number">0</span>, <span class="string">'a'</span>))); |
| <span class="macro">assert_eq!</span>(stream.next().<span class="kw">await</span>, <span class="prelude-val">Some</span>((<span class="number">1</span>, <span class="string">'b'</span>))); |
| <span class="macro">assert_eq!</span>(stream.next().<span class="kw">await</span>, <span class="prelude-val">Some</span>((<span class="number">2</span>, <span class="string">'c'</span>))); |
| <span class="macro">assert_eq!</span>(stream.next().<span class="kw">await</span>, <span class="prelude-val">None</span>);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.filter" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#417-424">source</a><h4 class="code-header">fn <a href="#method.filter" class="fnname">filter</a><Fut, F>(self, f: F) -> <a class="struct" href="struct.Filter.html" title="struct futures_util::stream::Filter">Filter</a><Self, Fut, F><span class="where fmt-newline">where<br> F: FnMut(&Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>) -> Fut,<br> Fut: Future<Output = bool>,<br> Self: Sized,</span></h4></section></summary><div class="docblock"><p>Filters the values produced by this stream according to the provided |
| asynchronous predicate.</p> |
| <p>As values of this stream are made available, the provided predicate <code>f</code> |
| will be run against them. If the predicate returns a <code>Future</code> which |
| resolves to <code>true</code>, then the stream will yield the value, but if the |
| predicate returns a <code>Future</code> which resolves to <code>false</code>, then the value |
| will be discarded and the next value will be produced.</p> |
| <p>Note that this function consumes the stream passed into it and returns a |
| wrapped version of it, similar to the existing <code>filter</code> methods in the |
| standard library.</p> |
| <h5 id="examples-4"><a href="#examples-4">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::future; |
| <span class="kw">use </span>futures::stream::{<span class="self">self</span>, StreamExt}; |
| |
| <span class="kw">let </span>stream = stream::iter(<span class="number">1</span>..=<span class="number">10</span>); |
| <span class="kw">let </span>events = stream.filter(|x| future::ready(x % <span class="number">2 </span>== <span class="number">0</span>)); |
| |
| <span class="macro">assert_eq!</span>(<span class="macro">vec!</span>[<span class="number">2</span>, <span class="number">4</span>, <span class="number">6</span>, <span class="number">8</span>, <span class="number">10</span>], events.collect::<Vec<<span class="kw">_</span>>>().<span class="kw">await</span>);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.filter_map" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#451-458">source</a><h4 class="code-header">fn <a href="#method.filter_map" class="fnname">filter_map</a><Fut, T, F>(self, f: F) -> <a class="struct" href="struct.FilterMap.html" title="struct futures_util::stream::FilterMap">FilterMap</a><Self, Fut, F><span class="where fmt-newline">where<br> F: FnMut(Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>) -> Fut,<br> Fut: Future<Output = Option<T>>,<br> Self: Sized,</span></h4></section></summary><div class="docblock"><p>Filters the values produced by this stream while simultaneously mapping |
| them to a different type according to the provided asynchronous closure.</p> |
| <p>As values of this stream are made available, the provided function will |
| be run on them. If the future returned by the predicate <code>f</code> resolves to |
| <a href="Some"><code>Some(item)</code></a> then the stream will yield the value <code>item</code>, but if |
| it resolves to [<code>None</code>] then the next value will be produced.</p> |
| <p>Note that this function consumes the stream passed into it and returns a |
| wrapped version of it, similar to the existing <code>filter_map</code> methods in |
| the standard library.</p> |
| <h5 id="examples-5"><a href="#examples-5">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::stream::{<span class="self">self</span>, StreamExt}; |
| |
| <span class="kw">let </span>stream = stream::iter(<span class="number">1</span>..=<span class="number">10</span>); |
| <span class="kw">let </span>events = stream.filter_map(|x| <span class="kw">async move </span>{ |
| <span class="kw">if </span>x % <span class="number">2 </span>== <span class="number">0 </span>{ <span class="prelude-val">Some</span>(x + <span class="number">1</span>) } <span class="kw">else </span>{ <span class="prelude-val">None </span>} |
| }); |
| |
| <span class="macro">assert_eq!</span>(<span class="macro">vec!</span>[<span class="number">3</span>, <span class="number">5</span>, <span class="number">7</span>, <span class="number">9</span>, <span class="number">11</span>], events.collect::<Vec<<span class="kw">_</span>>>().<span class="kw">await</span>);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.then" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#482-489">source</a><h4 class="code-header">fn <a href="#method.then" class="fnname">then</a><Fut, F>(self, f: F) -> <a class="struct" href="struct.Then.html" title="struct futures_util::stream::Then">Then</a><Self, Fut, F><span class="where fmt-newline">where<br> F: FnMut(Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>) -> Fut,<br> Fut: Future,<br> Self: Sized,</span></h4></section></summary><div class="docblock"><p>Computes from this stream’s items new items of a different type using |
| an asynchronous closure.</p> |
| <p>The provided closure <code>f</code> will be called with an <code>Item</code> once a value is |
| ready, it returns a future which will then be run to completion |
| to produce the next value on this stream.</p> |
| <p>Note that this function consumes the stream passed into it and returns a |
| wrapped version of it.</p> |
| <h5 id="examples-6"><a href="#examples-6">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::stream::{<span class="self">self</span>, StreamExt}; |
| |
| <span class="kw">let </span>stream = stream::iter(<span class="number">1</span>..=<span class="number">3</span>); |
| <span class="kw">let </span>stream = stream.then(|x| <span class="kw">async move </span>{ x + <span class="number">3 </span>}); |
| |
| <span class="macro">assert_eq!</span>(<span class="macro">vec!</span>[<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>], stream.collect::<Vec<<span class="kw">_</span>>>().<span class="kw">await</span>);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.collect" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#516-521">source</a><h4 class="code-header">fn <a href="#method.collect" class="fnname">collect</a><C: Default + Extend<Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>>>(self) -> <a class="struct" href="struct.Collect.html" title="struct futures_util::stream::Collect">Collect</a><Self, C><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="struct.Collect.html" title="struct futures_util::stream::Collect">Collect</a><St, C></span><code class="content"><span class="where fmt-newline">impl<St, C> Future for <a class="struct" href="struct.Collect.html" title="struct futures_util::stream::Collect">Collect</a><St, C><span class="where fmt-newline">where<br> St: <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a>,<br> C: Default + Extend<St::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>>,</span></span><span class="where fmt-newline"> type <a class="associatedtype">Output</a> = C;</span></code></span></span></span></span><span class="where fmt-newline">where<br> Self: Sized,</span></h4></section></summary><div class="docblock"><p>Transforms a stream into a collection, returning a |
| future representing the result of that computation.</p> |
| <p>The returned future will be resolved when the stream terminates.</p> |
| <h5 id="examples-7"><a href="#examples-7">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::channel::mpsc; |
| <span class="kw">use </span>futures::stream::StreamExt; |
| <span class="kw">use </span>std::thread; |
| |
| <span class="kw">let </span>(tx, rx) = mpsc::unbounded(); |
| |
| thread::spawn(<span class="kw">move </span>|| { |
| <span class="kw">for </span>i <span class="kw">in </span><span class="number">1</span>..=<span class="number">5 </span>{ |
| tx.unbounded_send(i).unwrap(); |
| } |
| }); |
| |
| <span class="kw">let </span>output = rx.collect::<Vec<i32>>().<span class="kw">await</span>; |
| <span class="macro">assert_eq!</span>(output, <span class="macro">vec!</span>[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.unzip" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#553-560">source</a><h4 class="code-header">fn <a href="#method.unzip" class="fnname">unzip</a><A, B, FromA, FromB>(self) -> <a class="struct" href="struct.Unzip.html" title="struct futures_util::stream::Unzip">Unzip</a><Self, FromA, FromB><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="struct.Unzip.html" title="struct futures_util::stream::Unzip">Unzip</a><St, FromA, FromB></span><code class="content"><span class="where fmt-newline">impl<St, A, B, FromA, FromB> Future for <a class="struct" href="struct.Unzip.html" title="struct futures_util::stream::Unzip">Unzip</a><St, FromA, FromB><span class="where fmt-newline">where<br> St: <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a><Item = (A, B)>,<br> FromA: Default + Extend<A>,<br> FromB: Default + Extend<B>,</span></span><span class="where fmt-newline"> type <a class="associatedtype">Output</a> = (FromA, FromB);</span></code></span></span></span></span><span class="where fmt-newline">where<br> FromA: Default + Extend<A>,<br> FromB: Default + Extend<B>,<br> Self: Sized + <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a><Item = (A, B)>,</span></h4></section></summary><div class="docblock"><p>Converts a stream of pairs into a future, which |
| resolves to pair of containers.</p> |
| <p><code>unzip()</code> produces a future, which resolves to two |
| collections: one from the left elements of the pairs, |
| and one from the right elements.</p> |
| <p>The returned future will be resolved when the stream terminates.</p> |
| <h5 id="examples-8"><a href="#examples-8">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::channel::mpsc; |
| <span class="kw">use </span>futures::stream::StreamExt; |
| <span class="kw">use </span>std::thread; |
| |
| <span class="kw">let </span>(tx, rx) = mpsc::unbounded(); |
| |
| thread::spawn(<span class="kw">move </span>|| { |
| tx.unbounded_send((<span class="number">1</span>, <span class="number">2</span>)).unwrap(); |
| tx.unbounded_send((<span class="number">3</span>, <span class="number">4</span>)).unwrap(); |
| tx.unbounded_send((<span class="number">5</span>, <span class="number">6</span>)).unwrap(); |
| }); |
| |
| <span class="kw">let </span>(o1, o2): (Vec<<span class="kw">_</span>>, Vec<<span class="kw">_</span>>) = rx.unzip().<span class="kw">await</span>; |
| <span class="macro">assert_eq!</span>(o1, <span class="macro">vec!</span>[<span class="number">1</span>, <span class="number">3</span>, <span class="number">5</span>]); |
| <span class="macro">assert_eq!</span>(o2, <span class="macro">vec!</span>[<span class="number">2</span>, <span class="number">4</span>, <span class="number">6</span>]);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.concat" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#594-600">source</a><h4 class="code-header">fn <a href="#method.concat" class="fnname">concat</a>(self) -> <a class="struct" href="struct.Concat.html" title="struct futures_util::stream::Concat">Concat</a><Self><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="struct.Concat.html" title="struct futures_util::stream::Concat">Concat</a><St></span><code class="content"><span class="where fmt-newline">impl<St> Future for <a class="struct" href="struct.Concat.html" title="struct futures_util::stream::Concat">Concat</a><St><span class="where fmt-newline">where<br> St: <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a>,<br> St::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>: Extend<<St::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a> as IntoIterator>::Item> + IntoIterator + Default,</span></span><span class="where fmt-newline"> type <a class="associatedtype">Output</a> = St::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>;</span></code></span></span></span></span><span class="where fmt-newline">where<br> Self: Sized,<br> Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>: Extend<<<Self as <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a>>::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a> as IntoIterator>::Item> + IntoIterator + Default,</span></h4></section></summary><div class="docblock"><p>Concatenate all items of a stream into a single extendable |
| destination, returning a future representing the end result.</p> |
| <p>This combinator will extend the first item with the contents |
| of all the subsequent results of the stream. If the stream is |
| empty, the default value will be returned.</p> |
| <p>Works with all collections that implement the |
| <a href="std::iter::Extend"><code>Extend</code></a> trait.</p> |
| <h5 id="examples-9"><a href="#examples-9">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::channel::mpsc; |
| <span class="kw">use </span>futures::stream::StreamExt; |
| <span class="kw">use </span>std::thread; |
| |
| <span class="kw">let </span>(tx, rx) = mpsc::unbounded(); |
| |
| thread::spawn(<span class="kw">move </span>|| { |
| <span class="kw">for </span>i <span class="kw">in </span>(<span class="number">0</span>..<span class="number">3</span>).rev() { |
| <span class="kw">let </span>n = i * <span class="number">3</span>; |
| tx.unbounded_send(<span class="macro">vec!</span>[n + <span class="number">1</span>, n + <span class="number">2</span>, n + <span class="number">3</span>]).unwrap(); |
| } |
| }); |
| |
| <span class="kw">let </span>result = rx.concat().<span class="kw">await</span>; |
| |
| <span class="macro">assert_eq!</span>(result, <span class="macro">vec!</span>[<span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.count" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#627-632">source</a><h4 class="code-header">fn <a href="#method.count" class="fnname">count</a>(self) -> <a class="struct" href="struct.Count.html" title="struct futures_util::stream::Count">Count</a><Self><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="struct.Count.html" title="struct futures_util::stream::Count">Count</a><St></span><code class="content"><span class="where fmt-newline">impl<St: <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a>> Future for <a class="struct" href="struct.Count.html" title="struct futures_util::stream::Count">Count</a><St></span><span class="where fmt-newline"> type <a class="associatedtype">Output</a> = usize;</span></code></span></span></span></span><span class="where fmt-newline">where<br> Self: Sized,</span></h4></section></summary><div class="docblock"><p>Drives the stream to completion, counting the number of items.</p> |
| <h5 id="overflow-behavior-1"><a href="#overflow-behavior-1">Overflow Behavior</a></h5> |
| <p>The method does no guarding against overflows, so counting elements of a |
| stream with more than [<code>usize::MAX</code>] elements either produces the wrong |
| result or panics. If debug assertions are enabled, a panic is guaranteed.</p> |
| <h5 id="panics-1"><a href="#panics-1">Panics</a></h5> |
| <p>This function might panic if the iterator has more than [<code>usize::MAX</code>] |
| elements.</p> |
| <h5 id="examples-10"><a href="#examples-10">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::stream::{<span class="self">self</span>, StreamExt}; |
| |
| <span class="kw">let </span>stream = stream::iter(<span class="number">1</span>..=<span class="number">10</span>); |
| <span class="kw">let </span>count = stream.count().<span class="kw">await</span>; |
| |
| <span class="macro">assert_eq!</span>(count, <span class="number">10</span>);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.cycle" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#657-662">source</a><h4 class="code-header">fn <a href="#method.cycle" class="fnname">cycle</a>(self) -> <a class="struct" href="struct.Cycle.html" title="struct futures_util::stream::Cycle">Cycle</a><Self><span class="where fmt-newline">where<br> Self: Sized + Clone,</span></h4></section></summary><div class="docblock"><p>Repeats a stream endlessly.</p> |
| <p>The stream never terminates. Note that you likely want to avoid |
| usage of <code>collect</code> or such on the returned stream as it will exhaust |
| available memory as it tries to just fill up all RAM.</p> |
| <h5 id="examples-11"><a href="#examples-11">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::stream::{<span class="self">self</span>, StreamExt}; |
| <span class="kw">let </span>a = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]; |
| <span class="kw">let </span><span class="kw-2">mut </span>s = stream::iter(a.iter()).cycle(); |
| |
| <span class="macro">assert_eq!</span>(s.next().<span class="kw">await</span>, <span class="prelude-val">Some</span>(<span class="kw-2">&</span><span class="number">1</span>)); |
| <span class="macro">assert_eq!</span>(s.next().<span class="kw">await</span>, <span class="prelude-val">Some</span>(<span class="kw-2">&</span><span class="number">2</span>)); |
| <span class="macro">assert_eq!</span>(s.next().<span class="kw">await</span>, <span class="prelude-val">Some</span>(<span class="kw-2">&</span><span class="number">3</span>)); |
| <span class="macro">assert_eq!</span>(s.next().<span class="kw">await</span>, <span class="prelude-val">Some</span>(<span class="kw-2">&</span><span class="number">1</span>)); |
| <span class="macro">assert_eq!</span>(s.next().<span class="kw">await</span>, <span class="prelude-val">Some</span>(<span class="kw-2">&</span><span class="number">2</span>)); |
| <span class="macro">assert_eq!</span>(s.next().<span class="kw">await</span>, <span class="prelude-val">Some</span>(<span class="kw-2">&</span><span class="number">3</span>)); |
| <span class="macro">assert_eq!</span>(s.next().<span class="kw">await</span>, <span class="prelude-val">Some</span>(<span class="kw-2">&</span><span class="number">1</span>));</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.fold" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#684-691">source</a><h4 class="code-header">fn <a href="#method.fold" class="fnname">fold</a><T, Fut, F>(self, init: T, f: F) -> <a class="struct" href="struct.Fold.html" title="struct futures_util::stream::Fold">Fold</a><Self, Fut, T, F><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="struct.Fold.html" title="struct futures_util::stream::Fold">Fold</a><St, Fut, T, F></span><code class="content"><span class="where fmt-newline">impl<St, Fut, T, F> Future for <a class="struct" href="struct.Fold.html" title="struct futures_util::stream::Fold">Fold</a><St, Fut, T, F><span class="where fmt-newline">where<br> St: <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a>,<br> F: FnMut(T, St::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>) -> Fut,<br> Fut: Future<Output = T>,</span></span><span class="where fmt-newline"> type <a class="associatedtype">Output</a> = T;</span></code></span></span></span></span><span class="where fmt-newline">where<br> F: FnMut(T, Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>) -> Fut,<br> Fut: Future<Output = T>,<br> Self: Sized,</span></h4></section></summary><div class="docblock"><p>Execute an accumulating asynchronous computation over a stream, |
| collecting all the values into one final result.</p> |
| <p>This combinator will accumulate all values returned by this stream |
| according to the closure provided. The initial state is also provided to |
| this method and then is returned again by each execution of the closure. |
| Once the entire stream has been exhausted the returned future will |
| resolve to this value.</p> |
| <h5 id="examples-12"><a href="#examples-12">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::stream::{<span class="self">self</span>, StreamExt}; |
| |
| <span class="kw">let </span>number_stream = stream::iter(<span class="number">0</span>..<span class="number">6</span>); |
| <span class="kw">let </span>sum = number_stream.fold(<span class="number">0</span>, |acc, x| <span class="kw">async move </span>{ acc + x }); |
| <span class="macro">assert_eq!</span>(sum.<span class="kw">await</span>, <span class="number">15</span>);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.any" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#706-713">source</a><h4 class="code-header">fn <a href="#method.any" class="fnname">any</a><Fut, F>(self, f: F) -> <a class="struct" href="struct.Any.html" title="struct futures_util::stream::Any">Any</a><Self, Fut, F><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="struct.Any.html" title="struct futures_util::stream::Any">Any</a><St, Fut, F></span><code class="content"><span class="where fmt-newline">impl<St, Fut, F> Future for <a class="struct" href="struct.Any.html" title="struct futures_util::stream::Any">Any</a><St, Fut, F><span class="where fmt-newline">where<br> St: <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a>,<br> F: FnMut(St::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>) -> Fut,<br> Fut: Future<Output = bool>,</span></span><span class="where fmt-newline"> type <a class="associatedtype">Output</a> = bool;</span></code></span></span></span></span><span class="where fmt-newline">where<br> F: FnMut(Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>) -> Fut,<br> Fut: Future<Output = bool>,<br> Self: Sized,</span></h4></section></summary><div class="docblock"><p>Execute predicate over asynchronous stream, and return <code>true</code> if any element in stream satisfied a predicate.</p> |
| <h5 id="examples-13"><a href="#examples-13">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::stream::{<span class="self">self</span>, StreamExt}; |
| |
| <span class="kw">let </span>number_stream = stream::iter(<span class="number">0</span>..<span class="number">10</span>); |
| <span class="kw">let </span>contain_three = number_stream.any(|i| <span class="kw">async move </span>{ i == <span class="number">3 </span>}); |
| <span class="macro">assert_eq!</span>(contain_three.<span class="kw">await</span>, <span class="bool-val">true</span>);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.all" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#728-735">source</a><h4 class="code-header">fn <a href="#method.all" class="fnname">all</a><Fut, F>(self, f: F) -> <a class="struct" href="struct.All.html" title="struct futures_util::stream::All">All</a><Self, Fut, F><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="struct.All.html" title="struct futures_util::stream::All">All</a><St, Fut, F></span><code class="content"><span class="where fmt-newline">impl<St, Fut, F> Future for <a class="struct" href="struct.All.html" title="struct futures_util::stream::All">All</a><St, Fut, F><span class="where fmt-newline">where<br> St: <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a>,<br> F: FnMut(St::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>) -> Fut,<br> Fut: Future<Output = bool>,</span></span><span class="where fmt-newline"> type <a class="associatedtype">Output</a> = bool;</span></code></span></span></span></span><span class="where fmt-newline">where<br> F: FnMut(Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>) -> Fut,<br> Fut: Future<Output = bool>,<br> Self: Sized,</span></h4></section></summary><div class="docblock"><p>Execute predicate over asynchronous stream, and return <code>true</code> if all element in stream satisfied a predicate.</p> |
| <h5 id="examples-14"><a href="#examples-14">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::stream::{<span class="self">self</span>, StreamExt}; |
| |
| <span class="kw">let </span>number_stream = stream::iter(<span class="number">0</span>..<span class="number">10</span>); |
| <span class="kw">let </span>less_then_twenty = number_stream.all(|i| <span class="kw">async move </span>{ i < <span class="number">20 </span>}); |
| <span class="macro">assert_eq!</span>(less_then_twenty.<span class="kw">await</span>, <span class="bool-val">true</span>);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.flatten" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#768-774">source</a><h4 class="code-header">fn <a href="#method.flatten" class="fnname">flatten</a>(self) -> <a class="struct" href="struct.Flatten.html" title="struct futures_util::stream::Flatten">Flatten</a><Self><span class="where fmt-newline">where<br> Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>: <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a>,<br> Self: Sized,</span></h4></section></summary><div class="docblock"><p>Flattens a stream of streams into just one continuous stream.</p> |
| <h5 id="examples-15"><a href="#examples-15">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::channel::mpsc; |
| <span class="kw">use </span>futures::stream::StreamExt; |
| <span class="kw">use </span>std::thread; |
| |
| <span class="kw">let </span>(tx1, rx1) = mpsc::unbounded(); |
| <span class="kw">let </span>(tx2, rx2) = mpsc::unbounded(); |
| <span class="kw">let </span>(tx3, rx3) = mpsc::unbounded(); |
| |
| thread::spawn(<span class="kw">move </span>|| { |
| tx1.unbounded_send(<span class="number">1</span>).unwrap(); |
| tx1.unbounded_send(<span class="number">2</span>).unwrap(); |
| }); |
| thread::spawn(<span class="kw">move </span>|| { |
| tx2.unbounded_send(<span class="number">3</span>).unwrap(); |
| tx2.unbounded_send(<span class="number">4</span>).unwrap(); |
| }); |
| thread::spawn(<span class="kw">move </span>|| { |
| tx3.unbounded_send(rx1).unwrap(); |
| tx3.unbounded_send(rx2).unwrap(); |
| }); |
| |
| <span class="kw">let </span>output = rx3.flatten().collect::<Vec<i32>>().<span class="kw">await</span>; |
| <span class="macro">assert_eq!</span>(output, <span class="macro">vec!</span>[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.flatten_unordered" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#819-825">source</a><h4 class="code-header">fn <a href="#method.flatten_unordered" class="fnname">flatten_unordered</a>(<br> self,<br> limit: impl Into<Option<usize>><br>) -> <a class="type" href="type.FlattenUnordered.html" title="type futures_util::stream::FlattenUnordered">FlattenUnordered</a><Self><span class="where fmt-newline">where<br> Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>: <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a> + Unpin,<br> Self: Sized,</span></h4></section></summary><div class="docblock"><p>Flattens a stream of streams into just one continuous stream. Polls |
| inner streams produced by the base stream concurrently.</p> |
| <p>The only argument is an optional limit on the number of concurrently |
| polled streams. If this limit is not <code>None</code>, no more than <code>limit</code> streams |
| will be polled at the same time. The <code>limit</code> argument is of type |
| <code>Into<Option<usize>></code>, and so can be provided as either <code>None</code>, |
| <code>Some(10)</code>, or just <code>10</code>. Note: a limit of zero is interpreted as |
| no limit at all, and will have the same result as passing in <code>None</code>.</p> |
| <h5 id="examples-16"><a href="#examples-16">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::channel::mpsc; |
| <span class="kw">use </span>futures::stream::StreamExt; |
| <span class="kw">use </span>std::thread; |
| |
| <span class="kw">let </span>(tx1, rx1) = mpsc::unbounded(); |
| <span class="kw">let </span>(tx2, rx2) = mpsc::unbounded(); |
| <span class="kw">let </span>(tx3, rx3) = mpsc::unbounded(); |
| |
| thread::spawn(<span class="kw">move </span>|| { |
| tx1.unbounded_send(<span class="number">1</span>).unwrap(); |
| tx1.unbounded_send(<span class="number">2</span>).unwrap(); |
| }); |
| thread::spawn(<span class="kw">move </span>|| { |
| tx2.unbounded_send(<span class="number">3</span>).unwrap(); |
| tx2.unbounded_send(<span class="number">4</span>).unwrap(); |
| }); |
| thread::spawn(<span class="kw">move </span>|| { |
| tx3.unbounded_send(rx1).unwrap(); |
| tx3.unbounded_send(rx2).unwrap(); |
| }); |
| |
| <span class="kw">let </span><span class="kw-2">mut </span>output = rx3.flatten_unordered(<span class="prelude-val">None</span>).collect::<Vec<i32>>().<span class="kw">await</span>; |
| output.sort(); |
| |
| <span class="macro">assert_eq!</span>(output, <span class="macro">vec!</span>[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.flat_map" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#852-859">source</a><h4 class="code-header">fn <a href="#method.flat_map" class="fnname">flat_map</a><U, F>(self, f: F) -> <a class="struct" href="struct.FlatMap.html" title="struct futures_util::stream::FlatMap">FlatMap</a><Self, U, F><span class="where fmt-newline">where<br> F: FnMut(Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>) -> U,<br> U: <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a>,<br> Self: Sized,</span></h4></section></summary><div class="docblock"><p>Maps a stream like <a href="trait.StreamExt.html#method.map" title="StreamExt::map"><code>StreamExt::map</code></a> but flattens nested <code>Stream</code>s.</p> |
| <p><a href="trait.StreamExt.html#method.map" title="StreamExt::map"><code>StreamExt::map</code></a> is very useful, but if it produces a <code>Stream</code> instead, |
| you would have to chain combinators like <code>.map(f).flatten()</code> while this |
| combinator provides ability to write <code>.flat_map(f)</code> instead of chaining.</p> |
| <p>The provided closure which produces inner streams is executed over all elements |
| of stream as last inner stream is terminated and next stream item is available.</p> |
| <p>Note that this function consumes the stream passed into it and returns a |
| wrapped version of it, similar to the existing <code>flat_map</code> methods in the |
| standard library.</p> |
| <h5 id="examples-17"><a href="#examples-17">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::stream::{<span class="self">self</span>, StreamExt}; |
| |
| <span class="kw">let </span>stream = stream::iter(<span class="number">1</span>..=<span class="number">3</span>); |
| <span class="kw">let </span>stream = stream.flat_map(|x| stream::iter(<span class="macro">vec!</span>[x + <span class="number">3</span>; x])); |
| |
| <span class="macro">assert_eq!</span>(<span class="macro">vec!</span>[<span class="number">4</span>, <span class="number">5</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">6</span>, <span class="number">6</span>], stream.collect::<Vec<<span class="kw">_</span>>>().<span class="kw">await</span>);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.flat_map_unordered" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#901-912">source</a><h4 class="code-header">fn <a href="#method.flat_map_unordered" class="fnname">flat_map_unordered</a><U, F>(<br> self,<br> limit: impl Into<Option<usize>>,<br> f: F<br>) -> <a class="struct" href="struct.FlatMapUnordered.html" title="struct futures_util::stream::FlatMapUnordered">FlatMapUnordered</a><Self, U, F><span class="where fmt-newline">where<br> U: <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a> + Unpin,<br> F: FnMut(Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>) -> U,<br> Self: Sized,</span></h4></section></summary><div class="docblock"><p>Maps a stream like <a href="trait.StreamExt.html#method.map" title="StreamExt::map"><code>StreamExt::map</code></a> but flattens nested <code>Stream</code>s |
| and polls them concurrently, yielding items in any order, as they made |
| available.</p> |
| <p><a href="trait.StreamExt.html#method.map" title="StreamExt::map"><code>StreamExt::map</code></a> is very useful, but if it produces <code>Stream</code>s |
| instead, and you need to poll all of them concurrently, you would |
| have to use something like <code>for_each_concurrent</code> and merge values |
| by hand. This combinator provides ability to collect all values |
| from concurrently polled streams into one stream.</p> |
| <p>The first argument is an optional limit on the number of concurrently |
| polled streams. If this limit is not <code>None</code>, no more than <code>limit</code> streams |
| will be polled at the same time. The <code>limit</code> argument is of type |
| <code>Into<Option<usize>></code>, and so can be provided as either <code>None</code>, |
| <code>Some(10)</code>, or just <code>10</code>. Note: a limit of zero is interpreted as |
| no limit at all, and will have the same result as passing in <code>None</code>.</p> |
| <p>The provided closure which produces inner streams is executed over |
| all elements of stream as next stream item is available and limit |
| of concurrently processed streams isn’t exceeded.</p> |
| <p>Note that this function consumes the stream passed into it and |
| returns a wrapped version of it.</p> |
| <h5 id="examples-18"><a href="#examples-18">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::stream::{<span class="self">self</span>, StreamExt}; |
| |
| <span class="kw">let </span>stream = stream::iter(<span class="number">1</span>..<span class="number">5</span>); |
| <span class="kw">let </span>stream = stream.flat_map_unordered(<span class="number">1</span>, |x| stream::iter(<span class="macro">vec!</span>[x; x])); |
| <span class="kw">let </span><span class="kw-2">mut </span>values = stream.collect::<Vec<<span class="kw">_</span>>>().<span class="kw">await</span>; |
| values.sort(); |
| |
| <span class="macro">assert_eq!</span>(<span class="macro">vec!</span>[<span class="number">1usize</span>, <span class="number">2</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">3</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">4</span>, <span class="number">4</span>, <span class="number">4</span>], values);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.scan" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#938-945">source</a><h4 class="code-header">fn <a href="#method.scan" class="fnname">scan</a><S, B, Fut, F>(self, initial_state: S, f: F) -> <a class="struct" href="struct.Scan.html" title="struct futures_util::stream::Scan">Scan</a><Self, S, Fut, F><span class="where fmt-newline">where<br> F: FnMut(&mut S, Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>) -> Fut,<br> Fut: Future<Output = Option<B>>,<br> Self: Sized,</span></h4></section></summary><div class="docblock"><p>Combinator similar to <a href="trait.StreamExt.html#method.fold" title="StreamExt::fold"><code>StreamExt::fold</code></a> that holds internal state |
| and produces a new stream.</p> |
| <p>Accepts initial state and closure which will be applied to each element |
| of the stream until provided closure returns <code>None</code>. Once <code>None</code> is |
| returned, stream will be terminated.</p> |
| <h5 id="examples-19"><a href="#examples-19">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::future; |
| <span class="kw">use </span>futures::stream::{<span class="self">self</span>, StreamExt}; |
| |
| <span class="kw">let </span>stream = stream::iter(<span class="number">1</span>..=<span class="number">10</span>); |
| |
| <span class="kw">let </span>stream = stream.scan(<span class="number">0</span>, |state, x| { |
| <span class="kw-2">*</span>state += x; |
| future::ready(<span class="kw">if </span><span class="kw-2">*</span>state < <span class="number">10 </span>{ <span class="prelude-val">Some</span>(x) } <span class="kw">else </span>{ <span class="prelude-val">None </span>}) |
| }); |
| |
| <span class="macro">assert_eq!</span>(<span class="macro">vec!</span>[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>], stream.collect::<Vec<<span class="kw">_</span>>>().<span class="kw">await</span>);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.skip_while" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#969-976">source</a><h4 class="code-header">fn <a href="#method.skip_while" class="fnname">skip_while</a><Fut, F>(self, f: F) -> <a class="struct" href="struct.SkipWhile.html" title="struct futures_util::stream::SkipWhile">SkipWhile</a><Self, Fut, F><span class="where fmt-newline">where<br> F: FnMut(&Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>) -> Fut,<br> Fut: Future<Output = bool>,<br> Self: Sized,</span></h4></section></summary><div class="docblock"><p>Skip elements on this stream while the provided asynchronous predicate |
| resolves to <code>true</code>.</p> |
| <p>This function, like <code>Iterator::skip_while</code>, will skip elements on the |
| stream until the predicate <code>f</code> resolves to <code>false</code>. Once one element |
| returns <code>false</code>, all future elements will be returned from the underlying |
| stream.</p> |
| <h5 id="examples-20"><a href="#examples-20">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::future; |
| <span class="kw">use </span>futures::stream::{<span class="self">self</span>, StreamExt}; |
| |
| <span class="kw">let </span>stream = stream::iter(<span class="number">1</span>..=<span class="number">10</span>); |
| |
| <span class="kw">let </span>stream = stream.skip_while(|x| future::ready(<span class="kw-2">*</span>x <= <span class="number">5</span>)); |
| |
| <span class="macro">assert_eq!</span>(<span class="macro">vec!</span>[<span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>, <span class="number">10</span>], stream.collect::<Vec<<span class="kw">_</span>>>().<span class="kw">await</span>);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.take_while" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#999-1006">source</a><h4 class="code-header">fn <a href="#method.take_while" class="fnname">take_while</a><Fut, F>(self, f: F) -> <a class="struct" href="struct.TakeWhile.html" title="struct futures_util::stream::TakeWhile">TakeWhile</a><Self, Fut, F><span class="where fmt-newline">where<br> F: FnMut(&Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>) -> Fut,<br> Fut: Future<Output = bool>,<br> Self: Sized,</span></h4></section></summary><div class="docblock"><p>Take elements from this stream while the provided asynchronous predicate |
| resolves to <code>true</code>.</p> |
| <p>This function, like <code>Iterator::take_while</code>, will take elements from the |
| stream until the predicate <code>f</code> resolves to <code>false</code>. Once one element |
| returns <code>false</code>, it will always return that the stream is done.</p> |
| <h5 id="examples-21"><a href="#examples-21">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::future; |
| <span class="kw">use </span>futures::stream::{<span class="self">self</span>, StreamExt}; |
| |
| <span class="kw">let </span>stream = stream::iter(<span class="number">1</span>..=<span class="number">10</span>); |
| |
| <span class="kw">let </span>stream = stream.take_while(|x| future::ready(<span class="kw-2">*</span>x <= <span class="number">5</span>)); |
| |
| <span class="macro">assert_eq!</span>(<span class="macro">vec!</span>[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>], stream.collect::<Vec<<span class="kw">_</span>>>().<span class="kw">await</span>);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.take_until" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#1044-1050">source</a><h4 class="code-header">fn <a href="#method.take_until" class="fnname">take_until</a><Fut>(self, fut: Fut) -> <a class="struct" href="struct.TakeUntil.html" title="struct futures_util::stream::TakeUntil">TakeUntil</a><Self, Fut><span class="where fmt-newline">where<br> Fut: Future,<br> Self: Sized,</span></h4></section></summary><div class="docblock"><p>Take elements from this stream until the provided future resolves.</p> |
| <p>This function will take elements from the stream until the provided |
| stopping future <code>fut</code> resolves. Once the <code>fut</code> future becomes ready, |
| this stream combinator will always return that the stream is done.</p> |
| <p>The stopping future may return any type. Once the stream is stopped |
| the result of the stopping future may be accessed with <code>TakeUntil::take_result()</code>. |
| The stream may also be resumed with <code>TakeUntil::take_future()</code>. |
| See the documentation of <a href="struct.TakeUntil.html" title="TakeUntil"><code>TakeUntil</code></a> for more information.</p> |
| <h5 id="examples-22"><a href="#examples-22">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::future; |
| <span class="kw">use </span>futures::stream::{<span class="self">self</span>, StreamExt}; |
| <span class="kw">use </span>futures::task::Poll; |
| |
| <span class="kw">let </span>stream = stream::iter(<span class="number">1</span>..=<span class="number">10</span>); |
| |
| <span class="kw">let </span><span class="kw-2">mut </span>i = <span class="number">0</span>; |
| <span class="kw">let </span>stop_fut = future::poll_fn(|_cx| { |
| i += <span class="number">1</span>; |
| <span class="kw">if </span>i <= <span class="number">5 </span>{ |
| Poll::Pending |
| } <span class="kw">else </span>{ |
| Poll::Ready(()) |
| } |
| }); |
| |
| <span class="kw">let </span>stream = stream.take_until(stop_fut); |
| |
| <span class="macro">assert_eq!</span>(<span class="macro">vec!</span>[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>], stream.collect::<Vec<<span class="kw">_</span>>>().<span class="kw">await</span>);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.for_each" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#1085-1092">source</a><h4 class="code-header">fn <a href="#method.for_each" class="fnname">for_each</a><Fut, F>(self, f: F) -> <a class="struct" href="struct.ForEach.html" title="struct futures_util::stream::ForEach">ForEach</a><Self, Fut, F><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="struct.ForEach.html" title="struct futures_util::stream::ForEach">ForEach</a><St, Fut, F></span><code class="content"><span class="where fmt-newline">impl<St, Fut, F> Future for <a class="struct" href="struct.ForEach.html" title="struct futures_util::stream::ForEach">ForEach</a><St, Fut, F><span class="where fmt-newline">where<br> St: <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a>,<br> F: FnMut(St::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>) -> Fut,<br> Fut: Future<Output = ()>,</span></span><span class="where fmt-newline"> type <a class="associatedtype">Output</a> = ();</span></code></span></span></span></span><span class="where fmt-newline">where<br> F: FnMut(Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>) -> Fut,<br> Fut: Future<Output = ()>,<br> Self: Sized,</span></h4></section></summary><div class="docblock"><p>Runs this stream to completion, executing the provided asynchronous |
| closure for each element on the stream.</p> |
| <p>The closure provided will be called for each item this stream produces, |
| yielding a future. That future will then be executed to completion |
| before moving on to the next item.</p> |
| <p>The returned value is a <code>Future</code> where the <code>Output</code> type is <code>()</code>; it is |
| executed entirely for its side effects.</p> |
| <p>To process each item in the stream and produce another stream instead |
| of a single future, use <code>then</code> instead.</p> |
| <h5 id="examples-23"><a href="#examples-23">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::future; |
| <span class="kw">use </span>futures::stream::{<span class="self">self</span>, StreamExt}; |
| |
| <span class="kw">let </span><span class="kw-2">mut </span>x = <span class="number">0</span>; |
| |
| { |
| <span class="kw">let </span>fut = stream::repeat(<span class="number">1</span>).take(<span class="number">3</span>).for_each(|item| { |
| x += item; |
| future::ready(()) |
| }); |
| fut.<span class="kw">await</span>; |
| } |
| |
| <span class="macro">assert_eq!</span>(x, <span class="number">3</span>);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.for_each_concurrent" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#1141-1152">source</a><h4 class="code-header">fn <a href="#method.for_each_concurrent" class="fnname">for_each_concurrent</a><Fut, F>(<br> self,<br> limit: impl Into<Option<usize>>,<br> f: F<br>) -> <a class="struct" href="struct.ForEachConcurrent.html" title="struct futures_util::stream::ForEachConcurrent">ForEachConcurrent</a><Self, Fut, F><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="struct.ForEachConcurrent.html" title="struct futures_util::stream::ForEachConcurrent">ForEachConcurrent</a><St, Fut, F></span><code class="content"><span class="where fmt-newline">impl<St, Fut, F> Future for <a class="struct" href="struct.ForEachConcurrent.html" title="struct futures_util::stream::ForEachConcurrent">ForEachConcurrent</a><St, Fut, F><span class="where fmt-newline">where<br> St: <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a>,<br> F: FnMut(St::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>) -> Fut,<br> Fut: Future<Output = ()>,</span></span><span class="where fmt-newline"> type <a class="associatedtype">Output</a> = ();</span></code></span></span></span></span><span class="where fmt-newline">where<br> F: FnMut(Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>) -> Fut,<br> Fut: Future<Output = ()>,<br> Self: Sized,</span></h4></section></summary><div class="docblock"><p>Runs this stream to completion, executing the provided asynchronous |
| closure for each element on the stream concurrently as elements become |
| available.</p> |
| <p>This is similar to <a href="trait.StreamExt.html#method.for_each" title="StreamExt::for_each"><code>StreamExt::for_each</code></a>, but the futures |
| produced by the closure are run concurrently (but not in parallel– |
| this combinator does not introduce any threads).</p> |
| <p>The closure provided will be called for each item this stream produces, |
| yielding a future. That future will then be executed to completion |
| concurrently with the other futures produced by the closure.</p> |
| <p>The first argument is an optional limit on the number of concurrent |
| futures. If this limit is not <code>None</code>, no more than <code>limit</code> futures |
| will be run concurrently. The <code>limit</code> argument is of type |
| <code>Into<Option<usize>></code>, and so can be provided as either <code>None</code>, |
| <code>Some(10)</code>, or just <code>10</code>. Note: a limit of zero is interpreted as |
| no limit at all, and will have the same result as passing in <code>None</code>.</p> |
| <p>This method is only available when the <code>std</code> or <code>alloc</code> feature of this |
| library is activated, and it is activated by default.</p> |
| <h5 id="examples-24"><a href="#examples-24">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::channel::oneshot; |
| <span class="kw">use </span>futures::stream::{<span class="self">self</span>, StreamExt}; |
| |
| <span class="kw">let </span>(tx1, rx1) = oneshot::channel(); |
| <span class="kw">let </span>(tx2, rx2) = oneshot::channel(); |
| <span class="kw">let </span>(tx3, rx3) = oneshot::channel(); |
| |
| <span class="kw">let </span>fut = stream::iter(<span class="macro">vec!</span>[rx1, rx2, rx3]).for_each_concurrent( |
| <span class="comment">/* limit */ </span><span class="number">2</span>, |
| |rx| <span class="kw">async move </span>{ |
| rx.<span class="kw">await</span>.unwrap(); |
| } |
| ); |
| tx1.send(()).unwrap(); |
| tx2.send(()).unwrap(); |
| tx3.send(()).unwrap(); |
| fut.<span class="kw">await</span>;</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.take" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#1170-1175">source</a><h4 class="code-header">fn <a href="#method.take" class="fnname">take</a>(self, n: usize) -> <a class="struct" href="struct.Take.html" title="struct futures_util::stream::Take">Take</a><Self><span class="where fmt-newline">where<br> Self: Sized,</span></h4></section></summary><div class="docblock"><p>Creates a new stream of at most <code>n</code> items of the underlying stream.</p> |
| <p>Once <code>n</code> items have been yielded from this stream then it will always |
| return that the stream is done.</p> |
| <h5 id="examples-25"><a href="#examples-25">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::stream::{<span class="self">self</span>, StreamExt}; |
| |
| <span class="kw">let </span>stream = stream::iter(<span class="number">1</span>..=<span class="number">10</span>).take(<span class="number">3</span>); |
| |
| <span class="macro">assert_eq!</span>(<span class="macro">vec!</span>[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>], stream.collect::<Vec<<span class="kw">_</span>>>().<span class="kw">await</span>);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.skip" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#1193-1198">source</a><h4 class="code-header">fn <a href="#method.skip" class="fnname">skip</a>(self, n: usize) -> <a class="struct" href="struct.Skip.html" title="struct futures_util::stream::Skip">Skip</a><Self><span class="where fmt-newline">where<br> Self: Sized,</span></h4></section></summary><div class="docblock"><p>Creates a new stream which skips <code>n</code> items of the underlying stream.</p> |
| <p>Once <code>n</code> items have been skipped from this stream then it will always |
| return the remaining items on this stream.</p> |
| <h5 id="examples-26"><a href="#examples-26">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::stream::{<span class="self">self</span>, StreamExt}; |
| |
| <span class="kw">let </span>stream = stream::iter(<span class="number">1</span>..=<span class="number">10</span>).skip(<span class="number">5</span>); |
| |
| <span class="macro">assert_eq!</span>(<span class="macro">vec!</span>[<span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>, <span class="number">10</span>], stream.collect::<Vec<<span class="kw">_</span>>>().<span class="kw">await</span>);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.fuse" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#1239-1244">source</a><h4 class="code-header">fn <a href="#method.fuse" class="fnname">fuse</a>(self) -> <a class="struct" href="struct.Fuse.html" title="struct futures_util::stream::Fuse">Fuse</a><Self><span class="where fmt-newline">where<br> Self: Sized,</span></h4></section></summary><div class="docblock"><p>Fuse a stream such that <a href="trait.Stream.html#tymethod.poll_next"><code>poll_next</code></a> will never |
| again be called once it has finished. This method can be used to turn |
| any <code>Stream</code> into a <code>FusedStream</code>.</p> |
| <p>Normally, once a stream has returned [<code>None</code>] from |
| <a href="trait.Stream.html#tymethod.poll_next"><code>poll_next</code></a> any further calls could exhibit bad |
| behavior such as block forever, panic, never return, etc. If it is known |
| that <a href="trait.Stream.html#tymethod.poll_next"><code>poll_next</code></a> may be called after stream |
| has already finished, then this method can be used to ensure that it has |
| defined semantics.</p> |
| <p>The <a href="trait.Stream.html#tymethod.poll_next"><code>poll_next</code></a> method of a <code>fuse</code>d stream |
| is guaranteed to return [<code>None</code>] after the underlying stream has |
| finished.</p> |
| <h5 id="examples-27"><a href="#examples-27">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::executor::block_on_stream; |
| <span class="kw">use </span>futures::stream::{<span class="self">self</span>, StreamExt}; |
| <span class="kw">use </span>futures::task::Poll; |
| |
| <span class="kw">let </span><span class="kw-2">mut </span>x = <span class="number">0</span>; |
| <span class="kw">let </span>stream = stream::poll_fn(|<span class="kw">_</span>| { |
| x += <span class="number">1</span>; |
| <span class="kw">match </span>x { |
| <span class="number">0</span>..=<span class="number">2 </span>=> Poll::Ready(<span class="prelude-val">Some</span>(x)), |
| <span class="number">3 </span>=> Poll::Ready(<span class="prelude-val">None</span>), |
| <span class="kw">_ </span>=> <span class="macro">panic!</span>(<span class="string">"should not happen"</span>) |
| } |
| }).fuse(); |
| |
| <span class="kw">let </span><span class="kw-2">mut </span>iter = block_on_stream(stream); |
| <span class="macro">assert_eq!</span>(<span class="prelude-val">Some</span>(<span class="number">1</span>), iter.next()); |
| <span class="macro">assert_eq!</span>(<span class="prelude-val">Some</span>(<span class="number">2</span>), iter.next()); |
| <span class="macro">assert_eq!</span>(<span class="prelude-val">None</span>, iter.next()); |
| <span class="macro">assert_eq!</span>(<span class="prelude-val">None</span>, iter.next()); |
| <span class="comment">// ...</span></code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.by_ref" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#1272-1274">source</a><h4 class="code-header">fn <a href="#method.by_ref" class="fnname">by_ref</a>(&mut self) -> &mut Self</h4></section></summary><div class="docblock"><p>Borrows a stream, rather than consuming it.</p> |
| <p>This is useful to allow applying stream adaptors while still retaining |
| ownership of the original stream.</p> |
| <h5 id="examples-28"><a href="#examples-28">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::stream::{<span class="self">self</span>, StreamExt}; |
| |
| <span class="kw">let </span><span class="kw-2">mut </span>stream = stream::iter(<span class="number">1</span>..<span class="number">5</span>); |
| |
| <span class="kw">let </span>sum = stream.by_ref() |
| .take(<span class="number">2</span>) |
| .fold(<span class="number">0</span>, |a, b| <span class="kw">async move </span>{ a + b }) |
| .<span class="kw">await</span>; |
| <span class="macro">assert_eq!</span>(sum, <span class="number">3</span>); |
| |
| <span class="comment">// You can use the stream again |
| </span><span class="kw">let </span>sum = stream.take(<span class="number">2</span>) |
| .fold(<span class="number">0</span>, |a, b| <span class="kw">async move </span>{ a + b }) |
| .<span class="kw">await</span>; |
| <span class="macro">assert_eq!</span>(sum, <span class="number">7</span>);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.catch_unwind" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#1317-1322">source</a><h4 class="code-header">fn <a href="#method.catch_unwind" class="fnname">catch_unwind</a>(self) -> <a class="struct" href="struct.CatchUnwind.html" title="struct futures_util::stream::CatchUnwind">CatchUnwind</a><Self><span class="where fmt-newline">where<br> Self: Sized + UnwindSafe,</span></h4></section></summary><div class="docblock"><p>Catches unwinding panics while polling the stream.</p> |
| <p>Caught panic (if any) will be the last element of the resulting stream.</p> |
| <p>In general, panics within a stream can propagate all the way out to the |
| task level. This combinator makes it possible to halt unwinding within |
| the stream itself. It’s most commonly used within task executors. This |
| method should not be used for error handling.</p> |
| <p>Note that this method requires the <code>UnwindSafe</code> bound from the standard |
| library. This isn’t always applied automatically, and the standard |
| library provides an <code>AssertUnwindSafe</code> wrapper type to apply it |
| after-the fact. To assist using this method, the <a href="trait.Stream.html" title="Stream"><code>Stream</code></a> trait is |
| also implemented for <code>AssertUnwindSafe<St></code> where <code>St</code> implements |
| <a href="trait.Stream.html" title="Stream"><code>Stream</code></a>.</p> |
| <p>This method is only available when the <code>std</code> feature of this |
| library is activated, and it is activated by default.</p> |
| <h5 id="examples-29"><a href="#examples-29">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::stream::{<span class="self">self</span>, StreamExt}; |
| |
| <span class="kw">let </span>stream = stream::iter(<span class="macro">vec!</span>[<span class="prelude-val">Some</span>(<span class="number">10</span>), <span class="prelude-val">None</span>, <span class="prelude-val">Some</span>(<span class="number">11</span>)]); |
| <span class="comment">// Panic on second element |
| </span><span class="kw">let </span>stream_panicking = stream.map(|o| o.unwrap()); |
| <span class="comment">// Collect all the results |
| </span><span class="kw">let </span>stream = stream_panicking.catch_unwind(); |
| |
| <span class="kw">let </span>results: Vec<<span class="prelude-ty">Result</span><i32, <span class="kw">_</span>>> = stream.collect().<span class="kw">await</span>; |
| <span class="kw">match </span>results[<span class="number">0</span>] { |
| <span class="prelude-val">Ok</span>(<span class="number">10</span>) => {} |
| <span class="kw">_ </span>=> <span class="macro">panic!</span>(<span class="string">"unexpected result!"</span>), |
| } |
| <span class="macro">assert!</span>(results[<span class="number">1</span>].is_err()); |
| <span class="macro">assert_eq!</span>(results.len(), <span class="number">2</span>);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.boxed" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#1329-1334">source</a><h4 class="code-header">fn <a href="#method.boxed" class="fnname">boxed</a><'a>(self) -> <a class="type" href="type.BoxStream.html" title="type futures_util::stream::BoxStream">BoxStream</a><'a, Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>><span class="where fmt-newline">where<br> Self: Sized + Send + 'a,</span></h4></section></summary><div class="docblock"><p>Wrap the stream in a Box, pinning it.</p> |
| <p>This method is only available when the <code>std</code> or <code>alloc</code> feature of this |
| library is activated, and it is activated by default.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.boxed_local" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#1343-1348">source</a><h4 class="code-header">fn <a href="#method.boxed_local" class="fnname">boxed_local</a><'a>(self) -> <a class="type" href="type.LocalBoxStream.html" title="type futures_util::stream::LocalBoxStream">LocalBoxStream</a><'a, Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>><span class="where fmt-newline">where<br> Self: Sized + 'a,</span></h4></section></summary><div class="docblock"><p>Wrap the stream in a Box, pinning it.</p> |
| <p>Similar to <code>boxed</code>, but without the <code>Send</code> requirement.</p> |
| <p>This method is only available when the <code>std</code> or <code>alloc</code> feature of this |
| library is activated, and it is activated by default.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.buffered" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#1364-1370">source</a><h4 class="code-header">fn <a href="#method.buffered" class="fnname">buffered</a>(self, n: usize) -> <a class="struct" href="struct.Buffered.html" title="struct futures_util::stream::Buffered">Buffered</a><Self><span class="where fmt-newline">where<br> Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>: Future,<br> Self: Sized,</span></h4></section></summary><div class="docblock"><p>An adaptor for creating a buffered list of pending futures.</p> |
| <p>If this stream’s item can be converted into a future, then this adaptor |
| will buffer up to at most <code>n</code> futures and then return the outputs in the |
| same order as the underlying stream. No more than <code>n</code> futures will be |
| buffered at any point in time, and less than <code>n</code> may also be buffered |
| depending on the state of each future.</p> |
| <p>The returned stream will be a stream of each future’s output.</p> |
| <p>This method is only available when the <code>std</code> or <code>alloc</code> feature of this |
| library is activated, and it is activated by default.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.buffer_unordered" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#1409-1415">source</a><h4 class="code-header">fn <a href="#method.buffer_unordered" class="fnname">buffer_unordered</a>(self, n: usize) -> <a class="struct" href="struct.BufferUnordered.html" title="struct futures_util::stream::BufferUnordered">BufferUnordered</a><Self><span class="where fmt-newline">where<br> Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>: Future,<br> Self: Sized,</span></h4></section></summary><div class="docblock"><p>An adaptor for creating a buffered list of pending futures (unordered).</p> |
| <p>If this stream’s item can be converted into a future, then this adaptor |
| will buffer up to <code>n</code> futures and then return the outputs in the order |
| in which they complete. No more than <code>n</code> futures will be buffered at |
| any point in time, and less than <code>n</code> may also be buffered depending on |
| the state of each future.</p> |
| <p>The returned stream will be a stream of each future’s output.</p> |
| <p>This method is only available when the <code>std</code> or <code>alloc</code> feature of this |
| library is activated, and it is activated by default.</p> |
| <h5 id="examples-30"><a href="#examples-30">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::channel::oneshot; |
| <span class="kw">use </span>futures::stream::{<span class="self">self</span>, StreamExt}; |
| |
| <span class="kw">let </span>(send_one, recv_one) = oneshot::channel(); |
| <span class="kw">let </span>(send_two, recv_two) = oneshot::channel(); |
| |
| <span class="kw">let </span>stream_of_futures = stream::iter(<span class="macro">vec!</span>[recv_one, recv_two]); |
| <span class="kw">let </span><span class="kw-2">mut </span>buffered = stream_of_futures.buffer_unordered(<span class="number">10</span>); |
| |
| send_two.send(<span class="number">2i32</span>)<span class="question-mark">?</span>; |
| <span class="macro">assert_eq!</span>(buffered.next().<span class="kw">await</span>, <span class="prelude-val">Some</span>(<span class="prelude-val">Ok</span>(<span class="number">2i32</span>))); |
| |
| send_one.send(<span class="number">1i32</span>)<span class="question-mark">?</span>; |
| <span class="macro">assert_eq!</span>(buffered.next().<span class="kw">await</span>, <span class="prelude-val">Some</span>(<span class="prelude-val">Ok</span>(<span class="number">1i32</span>))); |
| |
| <span class="macro">assert_eq!</span>(buffered.next().<span class="kw">await</span>, <span class="prelude-val">None</span>);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.zip" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#1439-1445">source</a><h4 class="code-header">fn <a href="#method.zip" class="fnname">zip</a><St>(self, other: St) -> <a class="struct" href="struct.Zip.html" title="struct futures_util::stream::Zip">Zip</a><Self, St><span class="where fmt-newline">where<br> St: <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a>,<br> Self: Sized,</span></h4></section></summary><div class="docblock"><p>An adapter for zipping two streams together.</p> |
| <p>The zipped stream waits for both streams to produce an item, and then |
| returns that pair. If either stream ends then the zipped stream will |
| also end.</p> |
| <h5 id="examples-31"><a href="#examples-31">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::stream::{<span class="self">self</span>, StreamExt}; |
| |
| <span class="kw">let </span>stream1 = stream::iter(<span class="number">1</span>..=<span class="number">3</span>); |
| <span class="kw">let </span>stream2 = stream::iter(<span class="number">5</span>..=<span class="number">10</span>); |
| |
| <span class="kw">let </span>vec = stream1.zip(stream2) |
| .collect::<Vec<<span class="kw">_</span>>>() |
| .<span class="kw">await</span>; |
| <span class="macro">assert_eq!</span>(<span class="macro">vec!</span>[(<span class="number">1</span>, <span class="number">5</span>), (<span class="number">2</span>, <span class="number">6</span>), (<span class="number">3</span>, <span class="number">7</span>)], vec);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.chain" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#1470-1476">source</a><h4 class="code-header">fn <a href="#method.chain" class="fnname">chain</a><St>(self, other: St) -> <a class="struct" href="struct.Chain.html" title="struct futures_util::stream::Chain">Chain</a><Self, St><span class="where fmt-newline">where<br> St: <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a><Item = Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>>,<br> Self: Sized,</span></h4></section></summary><div class="docblock"><p>Adapter for chaining two streams.</p> |
| <p>The resulting stream emits elements from the first stream, and when |
| first stream reaches the end, emits the elements from the second stream.</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::stream::{<span class="self">self</span>, StreamExt}; |
| |
| <span class="kw">let </span>stream1 = stream::iter(<span class="macro">vec!</span>[<span class="prelude-val">Ok</span>(<span class="number">10</span>), <span class="prelude-val">Err</span>(<span class="bool-val">false</span>)]); |
| <span class="kw">let </span>stream2 = stream::iter(<span class="macro">vec!</span>[<span class="prelude-val">Err</span>(<span class="bool-val">true</span>), <span class="prelude-val">Ok</span>(<span class="number">20</span>)]); |
| |
| <span class="kw">let </span>stream = stream1.chain(stream2); |
| |
| <span class="kw">let </span>result: Vec<<span class="kw">_</span>> = stream.collect().<span class="kw">await</span>; |
| <span class="macro">assert_eq!</span>(result, <span class="macro">vec!</span>[ |
| <span class="prelude-val">Ok</span>(<span class="number">10</span>), |
| <span class="prelude-val">Err</span>(<span class="bool-val">false</span>), |
| <span class="prelude-val">Err</span>(<span class="bool-val">true</span>), |
| <span class="prelude-val">Ok</span>(<span class="number">20</span>), |
| ]);</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.peekable" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#1481-1486">source</a><h4 class="code-header">fn <a href="#method.peekable" class="fnname">peekable</a>(self) -> <a class="struct" href="struct.Peekable.html" title="struct futures_util::stream::Peekable">Peekable</a><Self><span class="where fmt-newline">where<br> Self: Sized,</span></h4></section></summary><div class="docblock"><p>Creates a new stream which exposes a <code>peek</code> method.</p> |
| <p>Calling <code>peek</code> returns a reference to the next item in the stream.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.chunks" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#1507-1512">source</a><h4 class="code-header">fn <a href="#method.chunks" class="fnname">chunks</a>(self, capacity: usize) -> <a class="struct" href="struct.Chunks.html" title="struct futures_util::stream::Chunks">Chunks</a><Self><span class="where fmt-newline">where<br> Self: Sized,</span></h4></section></summary><div class="docblock"><p>An adaptor for chunking up items of the stream inside a vector.</p> |
| <p>This combinator will attempt to pull items from this stream and buffer |
| them into a local vector. At most <code>capacity</code> items will get buffered |
| before they’re yielded from the returned stream.</p> |
| <p>Note that the vectors returned from this iterator may not always have |
| <code>capacity</code> elements. If the underlying stream ended and only a partial |
| vector was created, it’ll be returned. Additionally if an error happens |
| from the underlying stream then the currently buffered items will be |
| yielded.</p> |
| <p>This method is only available when the <code>std</code> or <code>alloc</code> feature of this |
| library is activated, and it is activated by default.</p> |
| <h5 id="panics-2"><a href="#panics-2">Panics</a></h5> |
| <p>This method will panic if <code>capacity</code> is zero.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.ready_chunks" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#1532-1537">source</a><h4 class="code-header">fn <a href="#method.ready_chunks" class="fnname">ready_chunks</a>(self, capacity: usize) -> <a class="struct" href="struct.ReadyChunks.html" title="struct futures_util::stream::ReadyChunks">ReadyChunks</a><Self><span class="where fmt-newline">where<br> Self: Sized,</span></h4></section></summary><div class="docblock"><p>An adaptor for chunking up ready items of the stream inside a vector.</p> |
| <p>This combinator will attempt to pull ready items from this stream and |
| buffer them into a local vector. At most <code>capacity</code> items will get |
| buffered before they’re yielded from the returned stream. If underlying |
| stream returns <code>Poll::Pending</code>, and collected chunk is not empty, it will |
| be immediately returned.</p> |
| <p>If the underlying stream ended and only a partial vector was created, |
| it will be returned.</p> |
| <p>This method is only available when the <code>std</code> or <code>alloc</code> feature of this |
| library is activated, and it is activated by default.</p> |
| <h5 id="panics-3"><a href="#panics-3">Panics</a></h5> |
| <p>This method will panic if <code>capacity</code> is zero.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.forward" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#1552-1561">source</a><h4 class="code-header">fn <a href="#method.forward" class="fnname">forward</a><S>(self, sink: S) -> <a class="struct" href="struct.Forward.html" title="struct futures_util::stream::Forward">Forward</a><Self, S><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="struct.Forward.html" title="struct futures_util::stream::Forward">Forward</a><St, Si></span><code class="content"><span class="where fmt-newline">impl<St, Si> Future for <a class="struct" href="struct.Forward.html" title="struct futures_util::stream::Forward">Forward</a><St, Si><span class="where fmt-newline">where<br> Forward<St, Si, St::<a class="associatedtype" href="trait.TryStream.html#associatedtype.Ok" title="type futures_util::stream::TryStream::Ok">Ok</a>>: Future,<br> St: <a class="trait" href="trait.TryStream.html" title="trait futures_util::stream::TryStream">TryStream</a>,</span></span><span class="where fmt-newline"> type <a class="associatedtype">Output</a> = <Forward<St, Si, St::<a class="associatedtype" href="trait.TryStream.html#associatedtype.Ok" title="type futures_util::stream::TryStream::Ok">Ok</a>> as Future>::Output;</span></code></span></span></span></span><span class="where fmt-newline">where<br> S: <a class="trait" href="../sink/trait.Sink.html" title="trait futures_util::sink::Sink">Sink</a><Self::<a class="associatedtype" href="trait.TryStream.html#associatedtype.Ok" title="type futures_util::stream::TryStream::Ok">Ok</a>, Error = Self::<a class="associatedtype" href="trait.TryStream.html#associatedtype.Error" title="type futures_util::stream::TryStream::Error">Error</a>>,<br> Self: <a class="trait" href="trait.TryStream.html" title="trait futures_util::stream::TryStream">TryStream</a> + Sized,</span></h4></section></summary><div class="docblock"><p>A future that completes after the given stream has been fully processed |
| into the sink and the sink has been flushed and closed.</p> |
| <p>This future will drive the stream to keep producing items until it is |
| exhausted, sending each item to the sink. It will complete once the |
| stream is exhausted, the sink has received and flushed all items, and |
| the sink is closed. Note that neither the original stream nor provided |
| sink will be output by this future. Pass the sink by <code>Pin<&mut S></code> |
| (for example, via <code>forward(&mut sink)</code> inside an <code>async</code> fn/block) in |
| order to preserve access to the <code>Sink</code>. If the stream produces an error, |
| that error will be returned by this future without flushing/closing the sink.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.split" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#1576-1585">source</a><h4 class="code-header">fn <a href="#method.split" class="fnname">split</a><Item>(self) -> (<a class="struct" href="struct.SplitSink.html" title="struct futures_util::stream::SplitSink">SplitSink</a><Self, Item>, <a class="struct" href="struct.SplitStream.html" title="struct futures_util::stream::SplitStream">SplitStream</a><Self>)<span class="where fmt-newline">where<br> Self: <a class="trait" href="../sink/trait.Sink.html" title="trait futures_util::sink::Sink">Sink</a><Item> + Sized,</span></h4></section></summary><div class="docblock"><p>Splits this <code>Stream + Sink</code> object into separate <code>Sink</code> and <code>Stream</code> |
| objects.</p> |
| <p>This can be useful when you want to split ownership between tasks, or |
| allow direct interaction between the two objects (e.g. via |
| <code>Sink::send_all</code>).</p> |
| <p>This method is only available when the <code>std</code> or <code>alloc</code> feature of this |
| library is activated, and it is activated by default.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.inspect" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#1592-1598">source</a><h4 class="code-header">fn <a href="#method.inspect" class="fnname">inspect</a><F>(self, f: F) -> <a class="struct" href="struct.Inspect.html" title="struct futures_util::stream::Inspect">Inspect</a><Self, F><span class="where fmt-newline">where<br> F: FnMut(&Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>),<br> Self: Sized,</span></h4></section></summary><div class="docblock"><p>Do something with each item of this stream, afterwards passing it on.</p> |
| <p>This is similar to the <code>Iterator::inspect</code> method in the standard |
| library where it allows easily inspecting each value as it passes |
| through the stream, for example to debug what’s going on.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.left_stream" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#1605-1611">source</a><h4 class="code-header">fn <a href="#method.left_stream" class="fnname">left_stream</a><B>(self) -> <a class="enum" href="../future/enum.Either.html" title="enum futures_util::future::Either">Either</a><Self, B><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="enum" href="../future/enum.Either.html" title="enum futures_util::future::Either">Either</a><A, B></span><code class="content"><span class="where fmt-newline">impl<A, B> Future for <a class="enum" href="../future/enum.Either.html" title="enum futures_util::future::Either">Either</a><A, B><span class="where fmt-newline">where<br> A: Future,<br> B: Future<Output = A::Output>,</span></span><span class="where fmt-newline"> type <a class="associatedtype">Output</a> = A::Output;</span></code></span></span></span></span><span class="where fmt-newline">where<br> B: <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a><Item = Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>>,<br> Self: Sized,</span></h4></section></summary><div class="docblock"><p>Wrap this stream in an <code>Either</code> stream, making it the left-hand variant |
| of that <code>Either</code>.</p> |
| <p>This can be used in combination with the <code>right_stream</code> method to write <code>if</code> |
| statements that evaluate to different streams in different branches.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.right_stream" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#1618-1624">source</a><h4 class="code-header">fn <a href="#method.right_stream" class="fnname">right_stream</a><B>(self) -> <a class="enum" href="../future/enum.Either.html" title="enum futures_util::future::Either">Either</a><B, Self><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="enum" href="../future/enum.Either.html" title="enum futures_util::future::Either">Either</a><A, B></span><code class="content"><span class="where fmt-newline">impl<A, B> Future for <a class="enum" href="../future/enum.Either.html" title="enum futures_util::future::Either">Either</a><A, B><span class="where fmt-newline">where<br> A: Future,<br> B: Future<Output = A::Output>,</span></span><span class="where fmt-newline"> type <a class="associatedtype">Output</a> = A::Output;</span></code></span></span></span></span><span class="where fmt-newline">where<br> B: <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a><Item = Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>>,<br> Self: Sized,</span></h4></section></summary><div class="docblock"><p>Wrap this stream in an <code>Either</code> stream, making it the right-hand variant |
| of that <code>Either</code>.</p> |
| <p>This can be used in combination with the <code>left_stream</code> method to write <code>if</code> |
| statements that evaluate to different streams in different branches.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.poll_next_unpin" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#1628-1633">source</a><h4 class="code-header">fn <a href="#method.poll_next_unpin" class="fnname">poll_next_unpin</a>(&mut self, cx: &mut Context<'_>) -> Poll<Option<Self::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>>><span class="where fmt-newline">where<br> Self: Unpin,</span></h4></section></summary><div class="docblock"><p>A convenience method for calling <a href="trait.Stream.html#tymethod.poll_next" title="Stream::poll_next"><code>Stream::poll_next</code></a> on [<code>Unpin</code>] |
| stream types.</p> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.select_next_some" class="method has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#1685-1690">source</a><h4 class="code-header">fn <a href="#method.select_next_some" class="fnname">select_next_some</a>(&mut self) -> <a class="struct" href="struct.SelectNextSome.html" title="struct futures_util::stream::SelectNextSome">SelectNextSome</a><'_, Self><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="struct.SelectNextSome.html" title="struct futures_util::stream::SelectNextSome">SelectNextSome</a><'_, St></span><code class="content"><span class="where fmt-newline">impl<St: ?Sized + <a class="trait" href="trait.FusedStream.html" title="trait futures_util::stream::FusedStream">FusedStream</a> + Unpin> Future for <a class="struct" href="struct.SelectNextSome.html" title="struct futures_util::stream::SelectNextSome">SelectNextSome</a><'_, St></span><span class="where fmt-newline"> type <a class="associatedtype">Output</a> = St::<a class="associatedtype" href="trait.Stream.html#associatedtype.Item" title="type futures_util::stream::Stream::Item">Item</a>;</span></code></span></span></span></span><span class="where fmt-newline">where<br> Self: Unpin + <a class="trait" href="trait.FusedStream.html" title="trait futures_util::stream::FusedStream">FusedStream</a>,</span></h4></section></summary><div class="docblock"><p>Returns a [<code>Future</code>] that resolves when the next item in this stream is |
| ready.</p> |
| <p>This is similar to the <a href="trait.StreamExt.html#method.next" title="StreamExt::next"><code>next</code></a> method, but it won’t |
| resolve to [<code>None</code>] if used on an empty <a href="trait.Stream.html" title="Stream"><code>Stream</code></a>. Instead, the |
| returned future type will return <code>true</code> from |
| <a href="../future/trait.FusedFuture.html#tymethod.is_terminated"><code>FusedFuture::is_terminated</code></a> when the <a href="trait.Stream.html" title="Stream"><code>Stream</code></a> is empty, allowing |
| <a href="trait.StreamExt.html#method.select_next_some" title="StreamExt::select_next_some"><code>select_next_some</code></a> to be easily used with |
| the <a href="../macro.select.html"><code>select!</code></a> macro.</p> |
| <p>If the future is polled after this <a href="trait.Stream.html" title="Stream"><code>Stream</code></a> is empty it will panic. |
| Using the future with a <a href="../future/trait.FusedFuture.html"><code>FusedFuture</code></a>-aware primitive like the |
| <a href="../macro.select.html"><code>select!</code></a> macro will prevent this.</p> |
| <h5 id="examples-32"><a href="#examples-32">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::{future, select}; |
| <span class="kw">use </span>futures::stream::{StreamExt, FuturesUnordered}; |
| |
| <span class="kw">let </span><span class="kw-2">mut </span>fut = future::ready(<span class="number">1</span>); |
| <span class="kw">let </span><span class="kw-2">mut </span>async_tasks = FuturesUnordered::new(); |
| <span class="kw">let </span><span class="kw-2">mut </span>total = <span class="number">0</span>; |
| <span class="kw">loop </span>{ |
| <span class="macro">select! </span>{ |
| num = fut => { |
| <span class="comment">// First, the `ready` future completes. |
| </span>total += num; |
| <span class="comment">// Then we spawn a new task onto `async_tasks`, |
| </span>async_tasks.push(<span class="kw">async </span>{ <span class="number">5 </span>}); |
| }, |
| <span class="comment">// On the next iteration of the loop, the task we spawned |
| // completes. |
| </span>num = async_tasks.select_next_some() => { |
| total += num; |
| } |
| <span class="comment">// Finally, both the `ready` future and `async_tasks` have |
| // finished, so we enter the `complete` branch. |
| </span>complete => <span class="kw">break</span>, |
| } |
| } |
| <span class="macro">assert_eq!</span>(total, <span class="number">6</span>);</code></pre></div> |
| </div></details></div><h2 id="implementors" class="small-section-header">Implementors<a href="#implementors" class="anchor"></a></h2><div id="implementors-list"><section id="impl-StreamExt-for-T" class="impl has-srclink"><a class="srclink rightside" href="../../src/futures_util/stream/stream/mod.rs.html#245">source</a><a href="#impl-StreamExt-for-T" class="anchor"></a><h3 class="code-header">impl<T: ?Sized> <a class="trait" href="trait.StreamExt.html" title="trait futures_util::stream::StreamExt">StreamExt</a> for T<span class="where fmt-newline">where<br> T: <a class="trait" href="trait.Stream.html" title="trait futures_util::stream::Stream">Stream</a>,</span></h3></section></div><script src="../../implementors/futures_util/stream/stream/trait.StreamExt.js" async></script></section></div></main><div id="rustdoc-vars" data-root-path="../../" data-current-crate="futures_util" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html> |