blob: a611734d69c74663f36d203f98ce0efe18f34c8b [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, try_select"><title>try_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="#">try_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/try_select.rs.html#53-56">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 try_select&lt;A, B&gt;(future1: A, future2: B) -&gt; <a class="struct" href="../../future/struct.TrySelect.html" title="struct futures::future::TrySelect">TrySelect</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.TrySelect.html" title="struct futures::future::TrySelect">TrySelect</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.TrySelect.html" title="struct futures::future::TrySelect">TrySelect</a>&lt;A, B&gt;<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;A: Unpin + <a class="trait" href="../../future/trait.TryFuture.html" title="trait futures::future::TryFuture">TryFuture</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;B: Unpin + <a class="trait" href="../../future/trait.TryFuture.html" title="trait futures::future::TryFuture">TryFuture</a>,</span></span><span class="where fmt-newline"> type <a href="../../future/trait.Future.html#associatedtype.Output" class="associatedtype">Output</a> = Result&lt;<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.TryFuture.html" title="trait futures::future::TryFuture">TryFuture</a>&gt;::<a class="associatedtype" href="../../future/trait.TryFuture.html#associatedtype.Ok" title="type futures::future::TryFuture::Ok">Ok</a>, B), (&lt;B as <a class="trait" href="../../future/trait.TryFuture.html" title="trait futures::future::TryFuture">TryFuture</a>&gt;::<a class="associatedtype" href="../../future/trait.TryFuture.html#associatedtype.Ok" title="type futures::future::TryFuture::Ok">Ok</a>, A)&gt;, <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.TryFuture.html" title="trait futures::future::TryFuture">TryFuture</a>&gt;::<a class="associatedtype" href="../../future/trait.TryFuture.html#associatedtype.Error" title="type futures::future::TryFuture::Error">Error</a>, B), (&lt;B as <a class="trait" href="../../future/trait.TryFuture.html" title="trait futures::future::TryFuture">TryFuture</a>&gt;::<a class="associatedtype" href="../../future/trait.TryFuture.html#associatedtype.Error" title="type futures::future::TryFuture::Error">Error</a>, A)&gt;&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.TryFuture.html" title="trait futures::future::TryFuture">TryFuture</a> + Unpin,<br>&nbsp;&nbsp;&nbsp;&nbsp;B: <a class="trait" href="../../future/trait.TryFuture.html" title="trait futures::future::TryFuture">TryFuture</a> + Unpin,</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
success/error 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>
<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, TryFuture, TryFutureExt};
<span class="comment">// A poor-man&#39;s try_join implemented on top of select
</span><span class="kw">fn </span>try_join&lt;A, B, E&gt;(a: A, b: B) -&gt; <span class="kw">impl </span>TryFuture&lt;<span class="prelude-val">Ok</span>=(A::Ok, B::Ok), Error=E&gt;
<span class="kw">where </span>A: TryFuture&lt;Error = E&gt; + Unpin + <span class="lifetime">&#39;static</span>,
B: TryFuture&lt;Error = E&gt; + Unpin + <span class="lifetime">&#39;static</span>,
E: <span class="lifetime">&#39;static</span>,
{
future::try_select(a, b).then(|res| -&gt; Box&lt;<span class="kw">dyn </span>Future&lt;Output = <span class="prelude-ty">Result</span>&lt;<span class="kw">_</span>, <span class="kw">_</span>&gt;&gt; + Unpin&gt; {
<span class="kw">match </span>res {
<span class="prelude-val">Ok</span>(Either::Left((x, b))) =&gt; Box::new(b.map_ok(<span class="kw">move </span>|y| (x, y))),
<span class="prelude-val">Ok</span>(Either::Right((y, a))) =&gt; Box::new(a.map_ok(<span class="kw">move </span>|x| (x, y))),
<span class="prelude-val">Err</span>(Either::Left((e, <span class="kw">_</span>))) =&gt; Box::new(future::err(e)),
<span class="prelude-val">Err</span>(Either::Right((e, <span class="kw">_</span>))) =&gt; Box::new(future::err(e)),
}
})
}</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>