blob: 7efb3ae61b98e651fa468e3fc85fd067c820e69e [file] [log] [blame]
<!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="Waits for either one of two differently-typed futures to complete."><meta name="keywords" content="rust, rustlang, rust-lang, select"><title>select in futures::prelude::future - 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 fn"><!--[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">&#9776;</button><a class="sidebar-logo" href="../../../futures/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/index.html"><div class="logo-container"><img class="rust-logo" src="../../../rust-logo.svg" alt="logo"></div></a><div class="sidebar-elems"><h2><a href="index.html">In futures::prelude::future</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">Function <a href="../../index.html">futures</a>::<wbr><a href="../index.html">prelude</a>::<wbr><a href="index.html">future</a>::<wbr><a class="fn" href="#">select</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/future/select.rs.html#84-87">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">&#x2212;</span>]</a></span></div><div class="item-decl"><pre class="rust fn"><code>pub fn select&lt;A, B&gt;(future1: A, future2: B) -&gt; <a class="struct" href="../../future/struct.Select.html" title="struct futures::future::Select">Select</a>&lt;A, B&gt;<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="../../future/struct.Select.html" title="struct futures::future::Select">Select</a>&lt;A, B&gt;</span><code class="content"><span class="where fmt-newline">impl&lt;A, B&gt; <a class="trait" href="../../future/trait.Future.html" title="trait futures::future::Future">Future</a> for <a class="struct" href="../../future/struct.Select.html" title="struct futures::future::Select">Select</a>&lt;A, B&gt;<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;A: <a class="trait" href="../../future/trait.Future.html" title="trait futures::future::Future">Future</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Unpin.html" title="trait core::marker::Unpin">Unpin</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;B: <a class="trait" href="../../future/trait.Future.html" title="trait futures::future::Future">Future</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Unpin.html" title="trait core::marker::Unpin">Unpin</a>,</span></span><span class="where fmt-newline"> type <a href="../../future/trait.Future.html#associatedtype.Output" class="associatedtype">Output</a> = <a class="enum" href="../../future/enum.Either.html" title="enum futures::future::Either">Either</a>&lt;(&lt;A as <a class="trait" href="../../future/trait.Future.html" title="trait futures::future::Future">Future</a>&gt;::<a class="associatedtype" href="../../future/trait.Future.html#associatedtype.Output" title="type futures::future::Future::Output">Output</a>, B), (&lt;B as <a class="trait" href="../../future/trait.Future.html" title="trait futures::future::Future">Future</a>&gt;::<a class="associatedtype" href="../../future/trait.Future.html#associatedtype.Output" title="type futures::future::Future::Output">Output</a>, A)&gt;;</span></code></span></span></span></span><span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;A: <a class="trait" href="../../future/trait.Future.html" title="trait futures::future::Future">Future</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Unpin.html" title="trait core::marker::Unpin">Unpin</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;B: <a class="trait" href="../../future/trait.Future.html" title="trait futures::future::Future">Future</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Unpin.html" title="trait core::marker::Unpin">Unpin</a>,</span></code></pre></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Waits for either one of two differently-typed futures to complete.</p>
<p>This function will return a new future which awaits for either one of both
futures to complete. The returned future will finish with both the value
resolved and a future representing the completion of the other work.</p>
<p>Note that this function consumes the receiving futures and returns a
wrapped version of them.</p>
<p>Also note that if both this and the second future have the same
output type you can use the <code>Either::factor_first</code> method to
conveniently extract out the value at the end.</p>
<h2 id="examples"><a href="#examples">Examples</a></h2>
<p>A simple example</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::{
pin_mut,
future::Either,
future::<span class="self">self</span>,
};
<span class="comment">// These two futures have different types even though their outputs have the same type.
</span><span class="kw">let </span>future1 = <span class="kw">async </span>{
future::pending::&lt;()&gt;().<span class="kw">await</span>; <span class="comment">// will never finish
</span><span class="number">1
</span>};
<span class="kw">let </span>future2 = <span class="kw">async </span>{
future::ready(<span class="number">2</span>).<span class="kw">await
</span>};
<span class="comment">// &#39;select&#39; requires Future + Unpin bounds
</span><span class="macro">pin_mut!</span>(future1);
<span class="macro">pin_mut!</span>(future2);
<span class="kw">let </span>value = <span class="kw">match </span>future::select(future1, future2).<span class="kw">await </span>{
Either::Left((value1, <span class="kw">_</span>)) =&gt; value1, <span class="comment">// `value1` is resolved from `future1`
// `_` represents `future2`
</span>Either::Right((value2, <span class="kw">_</span>)) =&gt; value2, <span class="comment">// `value2` is resolved from `future2`
// `_` represents `future1`
</span>};
<span class="macro">assert!</span>(value == <span class="number">2</span>);</code></pre></div>
<p>A more complex example</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>futures::future::{<span class="self">self</span>, Either, Future, FutureExt};
<span class="comment">// A poor-man&#39;s join implemented on top of select
</span><span class="kw">fn </span>join&lt;A, B&gt;(a: A, b: B) -&gt; <span class="kw">impl </span>Future&lt;Output=(A::Output, B::Output)&gt;
<span class="kw">where </span>A: Future + Unpin,
B: Future + Unpin,
{
future::select(a, b).then(|either| {
<span class="kw">match </span>either {
Either::Left((x, b)) =&gt; b.map(<span class="kw">move </span>|y| (x, y)).left_future(),
Either::Right((y, a)) =&gt; a.map(<span class="kw">move </span>|x| (x, y)).right_future(),
}
})
}</code></pre></div>
</div></details></section></div></main><div id="rustdoc-vars" data-root-path="../../../" data-current-crate="futures" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html>