280 lines
64 KiB
HTML
280 lines
64 KiB
HTML
<!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="A collection of tasks spawned on a Tokio runtime."><title>JoinSet in tokio::task - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Italic-81dc35de.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-MediumItalic-ccf7e434.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2"href="../../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../../static.files/rustdoc-ca0dd0c4.css"><meta name="rustdoc-vars" data-root-path="../../" data-static-root-path="../../static.files/" data-current-crate="tokio" data-themes="" data-resource-suffix="" data-rustdoc-version="1.93.1 (01f6ddf75 2026-02-11) (Arch Linux rust 1:1.93.1-1)" data-channel="1.93.1" data-search-js="search-9e2438ea.js" data-stringdex-js="stringdex-a3946164.js" data-settings-js="settings-c38705f0.js" ><script src="../../static.files/storage-e2aeef58.js"></script><script defer src="sidebar-items.js"></script><script defer src="../../static.files/main-a410ff4d.js"></script><noscript><link rel="stylesheet" href="../../static.files/noscript-263c88ec.css"></noscript><link rel="alternate icon" type="image/png" href="../../static.files/favicon-32x32-eab170b8.png"><link rel="icon" type="image/svg+xml" href="../../static.files/favicon-044be391.svg"></head><body class="rustdoc struct"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><rustdoc-topbar><h2><a href="#">JoinSet</a></h2></rustdoc-topbar><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../../tokio/index.html">tokio</a><span class="version">1.49.0</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">JoinSet</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#examples" title="Examples">Examples</a></li><li><a href="#task-id-guarantees" title="Task ID guarantees">Task ID guarantees</a></li></ul><h3><a href="#implementations">Methods</a></h3><ul class="block method"><li><a href="#method.abort_all" title="abort_all">abort_all</a></li><li><a href="#method.detach_all" title="detach_all">detach_all</a></li><li><a href="#method.is_empty" title="is_empty">is_empty</a></li><li><a href="#method.join_all" title="join_all">join_all</a></li><li><a href="#method.join_next" title="join_next">join_next</a></li><li><a href="#method.join_next_with_id" title="join_next_with_id">join_next_with_id</a></li><li><a href="#method.len" title="len">len</a></li><li><a href="#method.new" title="new">new</a></li><li><a href="#method.poll_join_next" title="poll_join_next">poll_join_next</a></li><li><a href="#method.poll_join_next_with_id" title="poll_join_next_with_id">poll_join_next_with_id</a></li><li><a href="#method.shutdown" title="shutdown">shutdown</a></li><li><a href="#method.spawn" title="spawn">spawn</a></li><li><a href="#method.spawn_blocking" title="spawn_blocking">spawn_blocking</a></li><li><a href="#method.spawn_blocking_on" title="spawn_blocking_on">spawn_blocking_on</a></li><li><a href="#method.spawn_local" title="spawn_local">spawn_local</a></li><li><a href="#method.spawn_local_on" title="spawn_local_on">spawn_local_on</a></li><li><a href="#method.spawn_on" title="spawn_on">spawn_on</a></li><li><a href="#method.try_join_next" title="try_join_next">try_join_next</a></li><li><a href="#method.try_join_next_with_id" title="try_join_next_with_id">try_join_next_with_id</a></li></ul><h3><a href="#trait-implementations">Trait Implementations</a></h3><ul class="block trait-implementation"><li><a href="#impl-Debug-for-JoinSet%3CT%3E" title="Debug">Debug</a></li><li><a href="#impl-Default-for-JoinSet%3CT%3E" title="Default">Default</a></li><li><a href="#impl-Drop-for-JoinSet%3CT%3E" title="Drop">Drop</a></li><li><a href="#impl-Extend%3CF%3E-for-JoinSet%3CT%3E" title="Extend<F>">Extend<F></a></li><li><a href="#impl-FromIterator%3CF%3E-for-JoinSet%3CT%3E" title="FromIterator<F>">FromIterator<F></a></li></ul><h3><a href="#synthetic-implementations">Auto Trait Implementations</a></h3><ul class="block synthetic-implementation"><li><a href="#impl-RefUnwindSafe-for-JoinSet%3CT%3E" title="!RefUnwindSafe">!RefUnwindSafe</a></li><li><a href="#impl-UnwindSafe-for-JoinSet%3CT%3E" title="!UnwindSafe">!UnwindSafe</a></li><li><a href="#impl-Freeze-for-JoinSet%3CT%3E" title="Freeze">Freeze</a></li><li><a href="#impl-Send-for-JoinSet%3CT%3E" title="Send">Send</a></li><li><a href="#impl-Sync-for-JoinSet%3CT%3E" title="Sync">Sync</a></li><li><a href="#impl-Unpin-for-JoinSet%3CT%3E" title="Unpin">Unpin</a></li></ul><h3><a href="#blanket-implementations">Blanket Implementations</a></h3><ul class="block blanket-implementation"><li><a href="#impl-Any-for-T" title="Any">Any</a></li><li><a href="#impl-Borrow%3CT%3E-for-T" title="Borrow<T>">Borrow<T></a></li><li><a href="#impl-BorrowMut%3CT%3E-for-T" title="BorrowMut<T>">BorrowMut<T></a></li><li><a href="#impl-From%3CT%3E-for-T" title="From<T>">From<T></a></li><li><a href="#impl-Into%3CU%3E-for-T" title="Into<U>">Into<U></a></li><li><a href="#impl-TryFrom%3CU%3E-for-T" title="TryFrom<U>">TryFrom<U></a></li><li><a href="#impl-TryInto%3CU%3E-for-T" title="TryInto<U>">TryInto<U></a></li></ul></section><div id="rustdoc-modnav"><h2><a href="index.html">In tokio::<wbr>task</a></h2></div></div></nav><div class="sidebar-resizer" title="Drag to resize sidebar"></div><main><div class="width-limiter"><section id="main-content" class="content"><div class="main-heading"><div class="rustdoc-breadcrumbs"><a href="../index.html">tokio</a>::<wbr><a href="index.html">task</a></div><h1>Struct <span class="struct">JoinSet</span> <button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../../src/tokio/task/join_set.rs.html#64-66">Source</a> </span></div><pre class="rust item-decl"><code>pub struct JoinSet<T> { <span class="comment">/* private fields */</span> }</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>A collection of tasks spawned on a Tokio runtime.</p>
|
||
<p>A <code>JoinSet</code> can be used to await the completion of some or all of the tasks
|
||
in the set. The set is not ordered, and the tasks will be returned in the
|
||
order they complete.</p>
|
||
<p>All of the tasks must have the same return type <code>T</code>.</p>
|
||
<p>When the <code>JoinSet</code> is dropped, all tasks in the <code>JoinSet</code> are immediately aborted.</p>
|
||
<h2 id="examples"><a class="doc-anchor" href="#examples">§</a>Examples</h2>
|
||
<p>Spawn multiple tasks and wait for them.</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::task::JoinSet;
|
||
|
||
<span class="kw">let </span><span class="kw-2">mut </span>set = JoinSet::new();
|
||
|
||
<span class="kw">for </span>i <span class="kw">in </span><span class="number">0</span>..<span class="number">10 </span>{
|
||
set.spawn(<span class="kw">async move </span>{ i });
|
||
}
|
||
|
||
<span class="kw">let </span><span class="kw-2">mut </span>seen = [<span class="bool-val">false</span>; <span class="number">10</span>];
|
||
<span class="kw">while let </span><span class="prelude-val">Some</span>(res) = set.join_next().<span class="kw">await </span>{
|
||
<span class="kw">let </span>idx = res.unwrap();
|
||
seen[idx] = <span class="bool-val">true</span>;
|
||
}
|
||
|
||
<span class="kw">for </span>i <span class="kw">in </span><span class="number">0</span>..<span class="number">10 </span>{
|
||
<span class="macro">assert!</span>(seen[i]);
|
||
}</code></pre></div><h2 id="task-id-guarantees"><a class="doc-anchor" href="#task-id-guarantees">§</a>Task ID guarantees</h2>
|
||
<p>While a task is tracked in a <code>JoinSet</code>, that task’s ID is unique relative
|
||
to all other running tasks in Tokio. For this purpose, tracking a task in a
|
||
<code>JoinSet</code> is equivalent to holding a <a href="struct.JoinHandle.html" title="struct tokio::task::JoinHandle"><code>JoinHandle</code></a> to it. See the <a href="struct.Id.html" title="struct tokio::task::Id">task ID</a>
|
||
documentation for more info.</p>
|
||
</div></details><h2 id="implementations" class="section-header">Implementations<a href="#implementations" class="anchor">§</a></h2><div id="implementations-list"><details class="toggle implementors-toggle" open><summary><section id="impl-JoinSet%3CT%3E" class="impl"><a class="src rightside" href="../../src/tokio/task/join_set.rs.html#80-97">Source</a><a href="#impl-JoinSet%3CT%3E" class="anchor">§</a><h3 class="code-header">impl<T> <a class="struct" href="struct.JoinSet.html" title="struct tokio::task::JoinSet">JoinSet</a><T></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.new" class="method"><a class="src rightside" href="../../src/tokio/task/join_set.rs.html#82-86">Source</a><h4 class="code-header">pub fn <a href="#method.new" class="fn">new</a>() -> Self</h4></section></summary><div class="docblock"><p>Create a new <code>JoinSet</code>.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.len" class="method"><a class="src rightside" href="../../src/tokio/task/join_set.rs.html#89-91">Source</a><h4 class="code-header">pub fn <a href="#method.len" class="fn">len</a>(&self) -> <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.usize.html">usize</a></h4></section></summary><div class="docblock"><p>Returns the number of tasks currently in the <code>JoinSet</code>.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.is_empty" class="method"><a class="src rightside" href="../../src/tokio/task/join_set.rs.html#94-96">Source</a><h4 class="code-header">pub fn <a href="#method.is_empty" class="fn">is_empty</a>(&self) -> <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.bool.html">bool</a></h4></section></summary><div class="docblock"><p>Returns whether the <code>JoinSet</code> is empty.</p>
|
||
</div></details></div></details><details class="toggle implementors-toggle" open><summary><section id="impl-JoinSet%3CT%3E-1" class="impl"><a class="src rightside" href="../../src/tokio/task/join_set.rs.html#99-589">Source</a><a href="#impl-JoinSet%3CT%3E-1" class="anchor">§</a><h3 class="code-header">impl<T: 'static> <a class="struct" href="struct.JoinSet.html" title="struct tokio::task::JoinSet">JoinSet</a><T></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.spawn" class="method"><a class="src rightside" href="../../src/tokio/task/join_set.rs.html#142-149">Source</a><h4 class="code-header">pub fn <a href="#method.spawn" class="fn">spawn</a><F>(&mut self, task: F) -> <a class="struct" href="struct.AbortHandle.html" title="struct tokio::task::AbortHandle">AbortHandle</a><div class="where">where
|
||
F: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a><Output = T> + <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + 'static,
|
||
T: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a>,</div></h4></section></summary><div class="docblock"><p>Spawn the provided task on the <code>JoinSet</code>, returning an <a href="struct.AbortHandle.html" title="struct tokio::task::AbortHandle"><code>AbortHandle</code></a>
|
||
that can be used to remotely cancel the task.</p>
|
||
<p>The provided future will start running in the background immediately
|
||
when this method is called, even if you don’t await anything on this
|
||
<code>JoinSet</code>.</p>
|
||
<h5 id="panics"><a class="doc-anchor" href="#panics">§</a>Panics</h5>
|
||
<p>This method panics if called outside of a Tokio runtime.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.spawn_on" class="method"><a class="src rightside" href="../../src/tokio/task/join_set.rs.html#161-168">Source</a><h4 class="code-header">pub fn <a href="#method.spawn_on" class="fn">spawn_on</a><F>(&mut self, task: F, handle: &<a class="struct" href="../runtime/struct.Handle.html" title="struct tokio::runtime::Handle">Handle</a>) -> <a class="struct" href="struct.AbortHandle.html" title="struct tokio::task::AbortHandle">AbortHandle</a><div class="where">where
|
||
F: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a><Output = T> + <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + 'static,
|
||
T: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a>,</div></h4></section></summary><div class="docblock"><p>Spawn the provided task on the provided runtime and store it in this
|
||
<code>JoinSet</code> returning an <a href="struct.AbortHandle.html" title="struct tokio::task::AbortHandle"><code>AbortHandle</code></a> that can be used to remotely
|
||
cancel the task.</p>
|
||
<p>The provided future will start running in the background immediately
|
||
when this method is called, even if you don’t await anything on this
|
||
<code>JoinSet</code>.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.spawn_local" class="method"><a class="src rightside" href="../../src/tokio/task/join_set.rs.html#186-192">Source</a><h4 class="code-header">pub fn <a href="#method.spawn_local" class="fn">spawn_local</a><F>(&mut self, task: F) -> <a class="struct" href="struct.AbortHandle.html" title="struct tokio::task::AbortHandle">AbortHandle</a><div class="where">where
|
||
F: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a><Output = T> + 'static,</div></h4></section></summary><div class="docblock"><p>Spawn the provided task on the current <a href="struct.LocalSet.html" title="struct tokio::task::LocalSet"><code>LocalSet</code></a> or <a href="crate::runtime::LocalRuntime"><code>LocalRuntime</code></a>
|
||
and store it in this <code>JoinSet</code>, returning an <a href="struct.AbortHandle.html" title="struct tokio::task::AbortHandle"><code>AbortHandle</code></a> that can
|
||
be used to remotely cancel the task.</p>
|
||
<p>The provided future will start running in the background immediately
|
||
when this method is called, even if you don’t await anything on this
|
||
<code>JoinSet</code>.</p>
|
||
<h5 id="panics-1"><a class="doc-anchor" href="#panics-1">§</a>Panics</h5>
|
||
<p>This method panics if it is called outside of a <code>LocalSet</code> or <code>LocalRuntime</code>.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.spawn_local_on" class="method"><a class="src rightside" href="../../src/tokio/task/join_set.rs.html#206-212">Source</a><h4 class="code-header">pub fn <a href="#method.spawn_local_on" class="fn">spawn_local_on</a><F>(
|
||
&mut self,
|
||
task: F,
|
||
local_set: &<a class="struct" href="struct.LocalSet.html" title="struct tokio::task::LocalSet">LocalSet</a>,
|
||
) -> <a class="struct" href="struct.AbortHandle.html" title="struct tokio::task::AbortHandle">AbortHandle</a><div class="where">where
|
||
F: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a><Output = T> + 'static,</div></h4></section></summary><div class="docblock"><p>Spawn the provided task on the provided <a href="struct.LocalSet.html" title="struct tokio::task::LocalSet"><code>LocalSet</code></a> and store it in
|
||
this <code>JoinSet</code>, returning an <a href="struct.AbortHandle.html" title="struct tokio::task::AbortHandle"><code>AbortHandle</code></a> that can be used to
|
||
remotely cancel the task.</p>
|
||
<p>Unlike the <a href="struct.JoinSet.html#method.spawn_local" title="method tokio::task::JoinSet::spawn_local"><code>spawn_local</code></a> method, this method may be used to spawn local
|
||
tasks on a <code>LocalSet</code> that is <em>not</em> currently running. The provided
|
||
future will start running whenever the <code>LocalSet</code> is next started.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.spawn_blocking" class="method"><a class="src rightside" href="../../src/tokio/task/join_set.rs.html#254-261">Source</a><h4 class="code-header">pub fn <a href="#method.spawn_blocking" class="fn">spawn_blocking</a><F>(&mut self, f: F) -> <a class="struct" href="struct.AbortHandle.html" title="struct tokio::task::AbortHandle">AbortHandle</a><div class="where">where
|
||
F: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/ops/function/trait.FnOnce.html" title="trait core::ops::function::FnOnce">FnOnce</a>() -> T + <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + 'static,
|
||
T: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a>,</div></h4></section></summary><div class="docblock"><p>Spawn the blocking code on the blocking threadpool and store
|
||
it in this <code>JoinSet</code>, returning an <a href="struct.AbortHandle.html" title="struct tokio::task::AbortHandle"><code>AbortHandle</code></a> that can be
|
||
used to remotely cancel the task.</p>
|
||
<h5 id="examples-1"><a class="doc-anchor" href="#examples-1">§</a>Examples</h5>
|
||
<p>Spawn multiple blocking tasks and wait for them.</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::task::JoinSet;
|
||
|
||
<span class="attr">#[tokio::main]
|
||
</span><span class="kw">async fn </span>main() {
|
||
<span class="kw">let </span><span class="kw-2">mut </span>set = JoinSet::new();
|
||
|
||
<span class="kw">for </span>i <span class="kw">in </span><span class="number">0</span>..<span class="number">10 </span>{
|
||
set.spawn_blocking(<span class="kw">move </span>|| { i });
|
||
}
|
||
|
||
<span class="kw">let </span><span class="kw-2">mut </span>seen = [<span class="bool-val">false</span>; <span class="number">10</span>];
|
||
<span class="kw">while let </span><span class="prelude-val">Some</span>(res) = set.join_next().<span class="kw">await </span>{
|
||
<span class="kw">let </span>idx = res.unwrap();
|
||
seen[idx] = <span class="bool-val">true</span>;
|
||
}
|
||
|
||
<span class="kw">for </span>i <span class="kw">in </span><span class="number">0</span>..<span class="number">10 </span>{
|
||
<span class="macro">assert!</span>(seen[i]);
|
||
}
|
||
}</code></pre></div><h5 id="panics-2"><a class="doc-anchor" href="#panics-2">§</a>Panics</h5>
|
||
<p>This method panics if called outside of a Tokio runtime.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.spawn_blocking_on" class="method"><a class="src rightside" href="../../src/tokio/task/join_set.rs.html#269-276">Source</a><h4 class="code-header">pub fn <a href="#method.spawn_blocking_on" class="fn">spawn_blocking_on</a><F>(&mut self, f: F, handle: &<a class="struct" href="../runtime/struct.Handle.html" title="struct tokio::runtime::Handle">Handle</a>) -> <a class="struct" href="struct.AbortHandle.html" title="struct tokio::task::AbortHandle">AbortHandle</a><div class="where">where
|
||
F: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/ops/function/trait.FnOnce.html" title="trait core::ops::function::FnOnce">FnOnce</a>() -> T + <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + 'static,
|
||
T: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a>,</div></h4></section></summary><div class="docblock"><p>Spawn the blocking code on the blocking threadpool of the
|
||
provided runtime and store it in this <code>JoinSet</code>, returning an
|
||
<a href="struct.AbortHandle.html" title="struct tokio::task::AbortHandle"><code>AbortHandle</code></a> that can be used to remotely cancel the task.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.join_next" class="method"><a class="src rightside" href="../../src/tokio/task/join_set.rs.html#296-298">Source</a><h4 class="code-header">pub async fn <a href="#method.join_next" class="fn">join_next</a>(&mut self) -> <a class="enum" href="https://doc.rust-lang.org/1.93.1/core/option/enum.Option.html" title="enum core::option::Option">Option</a><<a class="enum" href="https://doc.rust-lang.org/1.93.1/core/result/enum.Result.html" title="enum core::result::Result">Result</a><T, <a class="struct" href="struct.JoinError.html" title="struct tokio::task::JoinError">JoinError</a>>></h4></section></summary><div class="docblock"><p>Waits until one of the tasks in the set completes and returns its output.</p>
|
||
<p>Returns <code>None</code> if the set is empty.</p>
|
||
<h5 id="cancel-safety"><a class="doc-anchor" href="#cancel-safety">§</a>Cancel Safety</h5>
|
||
<p>This method is cancel safe. If <code>join_next</code> is used as the event in a <code>tokio::select!</code>
|
||
statement and some other branch completes first, it is guaranteed that no tasks were
|
||
removed from this <code>JoinSet</code>.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.join_next_with_id" class="method"><a class="src rightside" href="../../src/tokio/task/join_set.rs.html#316-318">Source</a><h4 class="code-header">pub async fn <a href="#method.join_next_with_id" class="fn">join_next_with_id</a>(&mut self) -> <a class="enum" href="https://doc.rust-lang.org/1.93.1/core/option/enum.Option.html" title="enum core::option::Option">Option</a><<a class="enum" href="https://doc.rust-lang.org/1.93.1/core/result/enum.Result.html" title="enum core::result::Result">Result</a><(<a class="struct" href="struct.Id.html" title="struct tokio::task::Id">Id</a>, T), <a class="struct" href="struct.JoinError.html" title="struct tokio::task::JoinError">JoinError</a>>></h4></section></summary><div class="docblock"><p>Waits until one of the tasks in the set completes and returns its
|
||
output, along with the <a href="struct.Id.html" title="struct tokio::task::Id">task ID</a> of the completed task.</p>
|
||
<p>Returns <code>None</code> if the set is empty.</p>
|
||
<p>When this method returns an error, then the id of the task that failed can be accessed
|
||
using the <a href="struct.JoinError.html#method.id" title="method tokio::task::JoinError::id"><code>JoinError::id</code></a> method.</p>
|
||
<h5 id="cancel-safety-1"><a class="doc-anchor" href="#cancel-safety-1">§</a>Cancel Safety</h5>
|
||
<p>This method is cancel safe. If <code>join_next_with_id</code> is used as the event in a <code>tokio::select!</code>
|
||
statement and some other branch completes first, it is guaranteed that no tasks were
|
||
removed from this <code>JoinSet</code>.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.try_join_next" class="method"><a class="src rightside" href="../../src/tokio/task/join_set.rs.html#323-340">Source</a><h4 class="code-header">pub fn <a href="#method.try_join_next" class="fn">try_join_next</a>(&mut self) -> <a class="enum" href="https://doc.rust-lang.org/1.93.1/core/option/enum.Option.html" title="enum core::option::Option">Option</a><<a class="enum" href="https://doc.rust-lang.org/1.93.1/core/result/enum.Result.html" title="enum core::result::Result">Result</a><T, <a class="struct" href="struct.JoinError.html" title="struct tokio::task::JoinError">JoinError</a>>></h4></section></summary><div class="docblock"><p>Tries to join one of the tasks in the set that has completed and return its output.</p>
|
||
<p>Returns <code>None</code> if there are no completed tasks, or if the set is empty.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.try_join_next_with_id" class="method"><a class="src rightside" href="../../src/tokio/task/join_set.rs.html#352-369">Source</a><h4 class="code-header">pub fn <a href="#method.try_join_next_with_id" class="fn">try_join_next_with_id</a>(&mut self) -> <a class="enum" href="https://doc.rust-lang.org/1.93.1/core/option/enum.Option.html" title="enum core::option::Option">Option</a><<a class="enum" href="https://doc.rust-lang.org/1.93.1/core/result/enum.Result.html" title="enum core::result::Result">Result</a><(<a class="struct" href="struct.Id.html" title="struct tokio::task::Id">Id</a>, T), <a class="struct" href="struct.JoinError.html" title="struct tokio::task::JoinError">JoinError</a>>></h4></section></summary><div class="docblock"><p>Tries to join one of the tasks in the set that has completed and return its output,
|
||
along with the <a href="struct.Id.html" title="struct tokio::task::Id">task ID</a> of the completed task.</p>
|
||
<p>Returns <code>None</code> if there are no completed tasks, or if the set is empty.</p>
|
||
<p>When this method returns an error, then the id of the task that failed can be accessed
|
||
using the <a href="struct.JoinError.html#method.id" title="method tokio::task::JoinError::id"><code>JoinError::id</code></a> method.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.shutdown" class="method"><a class="src rightside" href="../../src/tokio/task/join_set.rs.html#381-384">Source</a><h4 class="code-header">pub async fn <a href="#method.shutdown" class="fn">shutdown</a>(&mut self)</h4></section></summary><div class="docblock"><p>Aborts all tasks and waits for them to finish shutting down.</p>
|
||
<p>Calling this method is equivalent to calling <a href="struct.JoinSet.html#method.abort_all" title="method tokio::task::JoinSet::abort_all"><code>abort_all</code></a> and then calling <a href="struct.JoinSet.html#method.join_next" title="method tokio::task::JoinSet::join_next"><code>join_next</code></a> in
|
||
a loop until it returns <code>None</code>.</p>
|
||
<p>This method ignores any panics in the tasks shutting down. When this call returns, the
|
||
<code>JoinSet</code> will be empty.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.join_all" class="method"><a class="src rightside" href="../../src/tokio/task/join_set.rs.html#446-457">Source</a><h4 class="code-header">pub async fn <a href="#method.join_all" class="fn">join_all</a>(self) -> <a class="struct" href="https://doc.rust-lang.org/1.93.1/alloc/vec/struct.Vec.html" title="struct alloc::vec::Vec">Vec</a><T></h4></section></summary><div class="docblock"><p>Awaits the completion of all tasks in this <code>JoinSet</code>, returning a vector of their results.</p>
|
||
<p>The results will be stored in the order they completed not the order they were spawned.
|
||
This is a convenience method that is equivalent to calling <a href="struct.JoinSet.html#method.join_next" title="method tokio::task::JoinSet::join_next"><code>join_next</code></a> in
|
||
a loop. If any tasks on the <code>JoinSet</code> fail with an <a href="struct.JoinError.html" title="struct tokio::task::JoinError"><code>JoinError</code></a>, then this call
|
||
to <code>join_all</code> will panic and all remaining tasks on the <code>JoinSet</code> are
|
||
cancelled. To handle errors in any other way, manually call <a href="struct.JoinSet.html#method.join_next" title="method tokio::task::JoinSet::join_next"><code>join_next</code></a>
|
||
in a loop.</p>
|
||
<h5 id="examples-2"><a class="doc-anchor" href="#examples-2">§</a>Examples</h5>
|
||
<p>Spawn multiple tasks and <code>join_all</code> them.</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::task::JoinSet;
|
||
<span class="kw">use </span>std::time::Duration;
|
||
|
||
<span class="kw">let </span><span class="kw-2">mut </span>set = JoinSet::new();
|
||
|
||
<span class="kw">for </span>i <span class="kw">in </span><span class="number">0</span>..<span class="number">3 </span>{
|
||
set.spawn(<span class="kw">async move </span>{
|
||
tokio::time::sleep(Duration::from_secs(<span class="number">3 </span>- i)).<span class="kw">await</span>;
|
||
i
|
||
});
|
||
}
|
||
|
||
<span class="kw">let </span>output = set.join_all().<span class="kw">await</span>;
|
||
<span class="macro">assert_eq!</span>(output, <span class="macro">vec!</span>[<span class="number">2</span>, <span class="number">1</span>, <span class="number">0</span>]);</code></pre></div>
|
||
<p>Equivalent implementation of <code>join_all</code>, using <a href="struct.JoinSet.html#method.join_next" title="method tokio::task::JoinSet::join_next"><code>join_next</code></a> and loop.</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::task::JoinSet;
|
||
<span class="kw">use </span>std::panic;
|
||
|
||
<span class="kw">let </span><span class="kw-2">mut </span>set = JoinSet::new();
|
||
|
||
<span class="kw">for </span>i <span class="kw">in </span><span class="number">0</span>..<span class="number">3 </span>{
|
||
set.spawn(<span class="kw">async move </span>{i});
|
||
}
|
||
|
||
<span class="kw">let </span><span class="kw-2">mut </span>output = Vec::new();
|
||
<span class="kw">while let </span><span class="prelude-val">Some</span>(res) = set.join_next().<span class="kw">await</span>{
|
||
<span class="kw">match </span>res {
|
||
<span class="prelude-val">Ok</span>(t) => output.push(t),
|
||
<span class="prelude-val">Err</span>(err) <span class="kw">if </span>err.is_panic() => panic::resume_unwind(err.into_panic()),
|
||
<span class="prelude-val">Err</span>(err) => <span class="macro">panic!</span>(<span class="string">"{err}"</span>),
|
||
}
|
||
}
|
||
<span class="macro">assert_eq!</span>(output.len(),<span class="number">3</span>);</code></pre></div></div></details><details class="toggle method-toggle" open><summary><section id="method.abort_all" class="method"><a class="src rightside" href="../../src/tokio/task/join_set.rs.html#463-465">Source</a><h4 class="code-header">pub fn <a href="#method.abort_all" class="fn">abort_all</a>(&mut self)</h4></section></summary><div class="docblock"><p>Aborts all tasks on this <code>JoinSet</code>.</p>
|
||
<p>This does not remove the tasks from the <code>JoinSet</code>. To wait for the tasks to complete
|
||
cancellation, you should call <code>join_next</code> in a loop until the <code>JoinSet</code> is empty.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.detach_all" class="method"><a class="src rightside" href="../../src/tokio/task/join_set.rs.html#471-473">Source</a><h4 class="code-header">pub fn <a href="#method.detach_all" class="fn">detach_all</a>(&mut self)</h4></section></summary><div class="docblock"><p>Removes all tasks from this <code>JoinSet</code> without aborting them.</p>
|
||
<p>The tasks removed by this call will continue to run in the background even if the <code>JoinSet</code>
|
||
is dropped.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.poll_join_next" class="method"><a class="src rightside" href="../../src/tokio/task/join_set.rs.html#500-527">Source</a><h4 class="code-header">pub fn <a href="#method.poll_join_next" class="fn">poll_join_next</a>(
|
||
&mut self,
|
||
cx: &mut <a class="struct" href="https://doc.rust-lang.org/1.93.1/core/task/wake/struct.Context.html" title="struct core::task::wake::Context">Context</a><'_>,
|
||
) -> <a class="enum" href="https://doc.rust-lang.org/1.93.1/core/task/poll/enum.Poll.html" title="enum core::task::poll::Poll">Poll</a><<a class="enum" href="https://doc.rust-lang.org/1.93.1/core/option/enum.Option.html" title="enum core::option::Option">Option</a><<a class="enum" href="https://doc.rust-lang.org/1.93.1/core/result/enum.Result.html" title="enum core::result::Result">Result</a><T, <a class="struct" href="struct.JoinError.html" title="struct tokio::task::JoinError">JoinError</a>>>></h4></section></summary><div class="docblock"><p>Polls for one of the tasks in the set to complete.</p>
|
||
<p>If this returns <code>Poll::Ready(Some(_))</code>, then the task that completed is removed from the set.</p>
|
||
<p>When the method returns <code>Poll::Pending</code>, the <code>Waker</code> in the provided <code>Context</code> is scheduled
|
||
to receive a wakeup when a task in the <code>JoinSet</code> completes. Note that on multiple calls to
|
||
<code>poll_join_next</code>, only the <code>Waker</code> from the <code>Context</code> passed to the most recent call is
|
||
scheduled to receive a wakeup.</p>
|
||
<h5 id="returns"><a class="doc-anchor" href="#returns">§</a>Returns</h5>
|
||
<p>This function returns:</p>
|
||
<ul>
|
||
<li><code>Poll::Pending</code> if the <code>JoinSet</code> is not empty but there is no task whose output is
|
||
available right now.</li>
|
||
<li><code>Poll::Ready(Some(Ok(value)))</code> if one of the tasks in this <code>JoinSet</code> has completed.
|
||
The <code>value</code> is the return value of one of the tasks that completed.</li>
|
||
<li><code>Poll::Ready(Some(Err(err)))</code> if one of the tasks in this <code>JoinSet</code> has panicked or been
|
||
aborted. The <code>err</code> is the <code>JoinError</code> from the panicked/aborted task.</li>
|
||
<li><code>Poll::Ready(None)</code> if the <code>JoinSet</code> is empty.</li>
|
||
</ul>
|
||
<p>Note that this method may return <code>Poll::Pending</code> even if one of the tasks has completed.
|
||
This can happen if the <a href="coop/index.html#cooperative-scheduling" title="mod tokio::task::coop">coop budget</a> is reached.</p>
|
||
</div></details><details class="toggle method-toggle" open><summary><section id="method.poll_join_next_with_id" class="method"><a class="src rightside" href="../../src/tokio/task/join_set.rs.html#556-588">Source</a><h4 class="code-header">pub fn <a href="#method.poll_join_next_with_id" class="fn">poll_join_next_with_id</a>(
|
||
&mut self,
|
||
cx: &mut <a class="struct" href="https://doc.rust-lang.org/1.93.1/core/task/wake/struct.Context.html" title="struct core::task::wake::Context">Context</a><'_>,
|
||
) -> <a class="enum" href="https://doc.rust-lang.org/1.93.1/core/task/poll/enum.Poll.html" title="enum core::task::poll::Poll">Poll</a><<a class="enum" href="https://doc.rust-lang.org/1.93.1/core/option/enum.Option.html" title="enum core::option::Option">Option</a><<a class="enum" href="https://doc.rust-lang.org/1.93.1/core/result/enum.Result.html" title="enum core::result::Result">Result</a><(<a class="struct" href="struct.Id.html" title="struct tokio::task::Id">Id</a>, T), <a class="struct" href="struct.JoinError.html" title="struct tokio::task::JoinError">JoinError</a>>>></h4></section></summary><div class="docblock"><p>Polls for one of the tasks in the set to complete.</p>
|
||
<p>If this returns <code>Poll::Ready(Some(_))</code>, then the task that completed is removed from the set.</p>
|
||
<p>When the method returns <code>Poll::Pending</code>, the <code>Waker</code> in the provided <code>Context</code> is scheduled
|
||
to receive a wakeup when a task in the <code>JoinSet</code> completes. Note that on multiple calls to
|
||
<code>poll_join_next</code>, only the <code>Waker</code> from the <code>Context</code> passed to the most recent call is
|
||
scheduled to receive a wakeup.</p>
|
||
<h5 id="returns-1"><a class="doc-anchor" href="#returns-1">§</a>Returns</h5>
|
||
<p>This function returns:</p>
|
||
<ul>
|
||
<li><code>Poll::Pending</code> if the <code>JoinSet</code> is not empty but there is no task whose output is
|
||
available right now.</li>
|
||
<li><code>Poll::Ready(Some(Ok((id, value))))</code> if one of the tasks in this <code>JoinSet</code> has completed.
|
||
The <code>value</code> is the return value of one of the tasks that completed, and
|
||
<code>id</code> is the <a href="struct.Id.html" title="struct tokio::task::Id">task ID</a> of that task.</li>
|
||
<li><code>Poll::Ready(Some(Err(err)))</code> if one of the tasks in this <code>JoinSet</code> has panicked or been
|
||
aborted. The <code>err</code> is the <code>JoinError</code> from the panicked/aborted task.</li>
|
||
<li><code>Poll::Ready(None)</code> if the <code>JoinSet</code> is empty.</li>
|
||
</ul>
|
||
<p>Note that this method may return <code>Poll::Pending</code> even if one of the tasks has completed.
|
||
This can happen if the <a href="coop/index.html#cooperative-scheduling" title="mod tokio::task::coop">coop budget</a> is reached.</p>
|
||
</div></details></div></details></div><h2 id="trait-implementations" class="section-header">Trait Implementations<a href="#trait-implementations" class="anchor">§</a></h2><div id="trait-implementations-list"><details class="toggle implementors-toggle" open><summary><section id="impl-Debug-for-JoinSet%3CT%3E" class="impl"><a class="src rightside" href="../../src/tokio/task/join_set.rs.html#597-601">Source</a><a href="#impl-Debug-for-JoinSet%3CT%3E" class="anchor">§</a><h3 class="code-header">impl<T> <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/fmt/trait.Debug.html" title="trait core::fmt::Debug">Debug</a> for <a class="struct" href="struct.JoinSet.html" title="struct tokio::task::JoinSet">JoinSet</a><T></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.fmt" class="method trait-impl"><a class="src rightside" href="../../src/tokio/task/join_set.rs.html#598-600">Source</a><a href="#method.fmt" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.93.1/core/fmt/trait.Debug.html#tymethod.fmt" class="fn">fmt</a>(&self, f: &mut <a class="struct" href="https://doc.rust-lang.org/1.93.1/core/fmt/struct.Formatter.html" title="struct core::fmt::Formatter">Formatter</a><'_>) -> <a class="type" href="https://doc.rust-lang.org/1.93.1/core/fmt/type.Result.html" title="type core::fmt::Result">Result</a></h4></section></summary><div class='docblock'>Formats the value using the given formatter. <a href="https://doc.rust-lang.org/1.93.1/core/fmt/trait.Debug.html#tymethod.fmt">Read more</a></div></details></div></details><details class="toggle implementors-toggle" open><summary><section id="impl-Default-for-JoinSet%3CT%3E" class="impl"><a class="src rightside" href="../../src/tokio/task/join_set.rs.html#603-607">Source</a><a href="#impl-Default-for-JoinSet%3CT%3E" class="anchor">§</a><h3 class="code-header">impl<T> <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/default/trait.Default.html" title="trait core::default::Default">Default</a> for <a class="struct" href="struct.JoinSet.html" title="struct tokio::task::JoinSet">JoinSet</a><T></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.default" class="method trait-impl"><a class="src rightside" href="../../src/tokio/task/join_set.rs.html#604-606">Source</a><a href="#method.default" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.93.1/core/default/trait.Default.html#tymethod.default" class="fn">default</a>() -> Self</h4></section></summary><div class='docblock'>Returns the “default value” for a type. <a href="https://doc.rust-lang.org/1.93.1/core/default/trait.Default.html#tymethod.default">Read more</a></div></details></div></details><details class="toggle implementors-toggle" open><summary><section id="impl-Drop-for-JoinSet%3CT%3E" class="impl"><a class="src rightside" href="../../src/tokio/task/join_set.rs.html#591-595">Source</a><a href="#impl-Drop-for-JoinSet%3CT%3E" class="anchor">§</a><h3 class="code-header">impl<T> <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/ops/drop/trait.Drop.html" title="trait core::ops::drop::Drop">Drop</a> for <a class="struct" href="struct.JoinSet.html" title="struct tokio::task::JoinSet">JoinSet</a><T></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.drop" class="method trait-impl"><a class="src rightside" href="../../src/tokio/task/join_set.rs.html#592-594">Source</a><a href="#method.drop" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.93.1/core/ops/drop/trait.Drop.html#tymethod.drop" class="fn">drop</a>(&mut self)</h4></section></summary><div class='docblock'>Executes the destructor for this type. <a href="https://doc.rust-lang.org/1.93.1/core/ops/drop/trait.Drop.html#tymethod.drop">Read more</a></div></details></div></details><details class="toggle implementors-toggle" open><summary><section id="impl-Extend%3CF%3E-for-JoinSet%3CT%3E" class="impl"><a class="src rightside" href="../../src/tokio/task/join_set.rs.html#681-695">Source</a><a href="#impl-Extend%3CF%3E-for-JoinSet%3CT%3E" class="anchor">§</a><h3 class="code-header">impl<T, F> <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/iter/traits/collect/trait.Extend.html" title="trait core::iter::traits::collect::Extend">Extend</a><F> for <a class="struct" href="struct.JoinSet.html" title="struct tokio::task::JoinSet">JoinSet</a><T><div class="where">where
|
||
F: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a><Output = T> + <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + 'static,
|
||
T: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + 'static,</div></h3><div class="docblock"><p>Extend a <a href="struct.JoinSet.html" title="struct tokio::task::JoinSet"><code>JoinSet</code></a> with futures from an iterator.</p>
|
||
</div></section></summary><div class="docblock"><p>This is equivalent to calling <a href="struct.JoinSet.html#method.spawn" title="method tokio::task::JoinSet::spawn"><code>JoinSet::spawn</code></a> on each element of the iterator.</p>
|
||
<h4 id="examples-4"><a class="doc-anchor" href="#examples-4">§</a>Examples</h4>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::task::JoinSet;
|
||
|
||
<span class="attr">#[tokio::main]
|
||
</span><span class="kw">async fn </span>main() {
|
||
<span class="kw">let </span><span class="kw-2">mut </span>set: JoinSet<<span class="kw">_</span>> = (<span class="number">0</span>..<span class="number">5</span>).map(|i| <span class="kw">async move </span>{ i }).collect();
|
||
|
||
set.extend((<span class="number">5</span>..<span class="number">10</span>).map(|i| <span class="kw">async move </span>{ i }));
|
||
|
||
<span class="kw">let </span><span class="kw-2">mut </span>seen = [<span class="bool-val">false</span>; <span class="number">10</span>];
|
||
<span class="kw">while let </span><span class="prelude-val">Some</span>(res) = set.join_next().<span class="kw">await </span>{
|
||
<span class="kw">let </span>idx = res.unwrap();
|
||
seen[idx] = <span class="bool-val">true</span>;
|
||
}
|
||
|
||
<span class="kw">for </span>i <span class="kw">in </span><span class="number">0</span>..<span class="number">10 </span>{
|
||
<span class="macro">assert!</span>(seen[i]);
|
||
}
|
||
}</code></pre></div></div><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.extend" class="method trait-impl"><a class="src rightside" href="../../src/tokio/task/join_set.rs.html#687-694">Source</a><a href="#method.extend" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.93.1/core/iter/traits/collect/trait.Extend.html#tymethod.extend" class="fn">extend</a><I>(&mut self, iter: I)<div class="where">where
|
||
I: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/iter/traits/collect/trait.IntoIterator.html" title="trait core::iter::traits::collect::IntoIterator">IntoIterator</a><Item = F>,</div></h4></section></summary><div class='docblock'>Extends a collection with the contents of an iterator. <a href="https://doc.rust-lang.org/1.93.1/core/iter/traits/collect/trait.Extend.html#tymethod.extend">Read more</a></div></details><details class="toggle method-toggle" open><summary><section id="method.extend_one" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.93.1/src/core/iter/traits/collect.rs.html#417">Source</a><a href="#method.extend_one" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.93.1/core/iter/traits/collect/trait.Extend.html#method.extend_one" class="fn">extend_one</a>(&mut self, item: A)</h4></section></summary><span class="item-info"><div class="stab unstable"><span class="emoji">🔬</span><span>This is a nightly-only experimental API. (<code>extend_one</code>)</span></div></span><div class='docblock'>Extends a collection with exactly one element.</div></details><details class="toggle method-toggle" open><summary><section id="method.extend_reserve" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.93.1/src/core/iter/traits/collect.rs.html#425">Source</a><a href="#method.extend_reserve" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.93.1/core/iter/traits/collect/trait.Extend.html#method.extend_reserve" class="fn">extend_reserve</a>(&mut self, additional: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.usize.html">usize</a>)</h4></section></summary><span class="item-info"><div class="stab unstable"><span class="emoji">🔬</span><span>This is a nightly-only experimental API. (<code>extend_one</code>)</span></div></span><div class='docblock'>Reserves capacity in a collection for the given number of additional elements. <a href="https://doc.rust-lang.org/1.93.1/core/iter/traits/collect/trait.Extend.html#method.extend_reserve">Read more</a></div></details></div></details><details class="toggle implementors-toggle" open><summary><section id="impl-FromIterator%3CF%3E-for-JoinSet%3CT%3E" class="impl"><a class="src rightside" href="../../src/tokio/task/join_set.rs.html#637-650">Source</a><a href="#impl-FromIterator%3CF%3E-for-JoinSet%3CT%3E" class="anchor">§</a><h3 class="code-header">impl<T, F> <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/iter/traits/collect/trait.FromIterator.html" title="trait core::iter::traits::collect::FromIterator">FromIterator</a><F> for <a class="struct" href="struct.JoinSet.html" title="struct tokio::task::JoinSet">JoinSet</a><T><div class="where">where
|
||
F: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a><Output = T> + <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + 'static,
|
||
T: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + 'static,</div></h3><div class="docblock"><p>Collect an iterator of futures into a <a href="struct.JoinSet.html" title="struct tokio::task::JoinSet"><code>JoinSet</code></a>.</p>
|
||
</div></section></summary><div class="docblock"><p>This is equivalent to calling <a href="struct.JoinSet.html#method.spawn" title="method tokio::task::JoinSet::spawn"><code>JoinSet::spawn</code></a> on each element of the iterator.</p>
|
||
<h4 id="examples-3"><a class="doc-anchor" href="#examples-3">§</a>Examples</h4>
|
||
<p>The main example from <a href="struct.JoinSet.html" title="struct tokio::task::JoinSet"><code>JoinSet</code></a>’s documentation can also be written using <a href="https://doc.rust-lang.org/1.93.1/core/iter/traits/iterator/trait.Iterator.html#method.collect" title="method core::iter::traits::iterator::Iterator::collect"><code>collect</code></a>:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tokio::task::JoinSet;
|
||
|
||
<span class="kw">let </span><span class="kw-2">mut </span>set: JoinSet<<span class="kw">_</span>> = (<span class="number">0</span>..<span class="number">10</span>).map(|i| <span class="kw">async move </span>{ i }).collect();
|
||
|
||
<span class="kw">let </span><span class="kw-2">mut </span>seen = [<span class="bool-val">false</span>; <span class="number">10</span>];
|
||
<span class="kw">while let </span><span class="prelude-val">Some</span>(res) = set.join_next().<span class="kw">await </span>{
|
||
<span class="kw">let </span>idx = res.unwrap();
|
||
seen[idx] = <span class="bool-val">true</span>;
|
||
}
|
||
|
||
<span class="kw">for </span>i <span class="kw">in </span><span class="number">0</span>..<span class="number">10 </span>{
|
||
<span class="macro">assert!</span>(seen[i]);
|
||
}</code></pre></div></div><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.from_iter" class="method trait-impl"><a class="src rightside" href="../../src/tokio/task/join_set.rs.html#643-649">Source</a><a href="#method.from_iter" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.93.1/core/iter/traits/collect/trait.FromIterator.html#tymethod.from_iter" class="fn">from_iter</a><I: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/iter/traits/collect/trait.IntoIterator.html" title="trait core::iter::traits::collect::IntoIterator">IntoIterator</a><Item = F>>(iter: I) -> Self</h4></section></summary><div class='docblock'>Creates a value from an iterator. <a href="https://doc.rust-lang.org/1.93.1/core/iter/traits/collect/trait.FromIterator.html#tymethod.from_iter">Read more</a></div></details></div></details></div><h2 id="synthetic-implementations" class="section-header">Auto Trait Implementations<a href="#synthetic-implementations" class="anchor">§</a></h2><div id="synthetic-implementations-list"><section id="impl-Freeze-for-JoinSet%3CT%3E" class="impl"><a href="#impl-Freeze-for-JoinSet%3CT%3E" class="anchor">§</a><h3 class="code-header">impl<T> <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Freeze.html" title="trait core::marker::Freeze">Freeze</a> for <a class="struct" href="struct.JoinSet.html" title="struct tokio::task::JoinSet">JoinSet</a><T></h3></section><section id="impl-RefUnwindSafe-for-JoinSet%3CT%3E" class="impl"><a href="#impl-RefUnwindSafe-for-JoinSet%3CT%3E" class="anchor">§</a><h3 class="code-header">impl<T> !<a class="trait" href="https://doc.rust-lang.org/1.93.1/core/panic/unwind_safe/trait.RefUnwindSafe.html" title="trait core::panic::unwind_safe::RefUnwindSafe">RefUnwindSafe</a> for <a class="struct" href="struct.JoinSet.html" title="struct tokio::task::JoinSet">JoinSet</a><T></h3></section><section id="impl-Send-for-JoinSet%3CT%3E" class="impl"><a href="#impl-Send-for-JoinSet%3CT%3E" class="anchor">§</a><h3 class="code-header">impl<T> <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> for <a class="struct" href="struct.JoinSet.html" title="struct tokio::task::JoinSet">JoinSet</a><T><div class="where">where
|
||
T: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a>,</div></h3></section><section id="impl-Sync-for-JoinSet%3CT%3E" class="impl"><a href="#impl-Sync-for-JoinSet%3CT%3E" class="anchor">§</a><h3 class="code-header">impl<T> <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> for <a class="struct" href="struct.JoinSet.html" title="struct tokio::task::JoinSet">JoinSet</a><T><div class="where">where
|
||
T: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a>,</div></h3></section><section id="impl-Unpin-for-JoinSet%3CT%3E" class="impl"><a href="#impl-Unpin-for-JoinSet%3CT%3E" class="anchor">§</a><h3 class="code-header">impl<T> <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Unpin.html" title="trait core::marker::Unpin">Unpin</a> for <a class="struct" href="struct.JoinSet.html" title="struct tokio::task::JoinSet">JoinSet</a><T></h3></section><section id="impl-UnwindSafe-for-JoinSet%3CT%3E" class="impl"><a href="#impl-UnwindSafe-for-JoinSet%3CT%3E" class="anchor">§</a><h3 class="code-header">impl<T> !<a class="trait" href="https://doc.rust-lang.org/1.93.1/core/panic/unwind_safe/trait.UnwindSafe.html" title="trait core::panic::unwind_safe::UnwindSafe">UnwindSafe</a> for <a class="struct" href="struct.JoinSet.html" title="struct tokio::task::JoinSet">JoinSet</a><T></h3></section></div><h2 id="blanket-implementations" class="section-header">Blanket Implementations<a href="#blanket-implementations" class="anchor">§</a></h2><div id="blanket-implementations-list"><details class="toggle implementors-toggle"><summary><section id="impl-Any-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.93.1/src/core/any.rs.html#138">Source</a><a href="#impl-Any-for-T" class="anchor">§</a><h3 class="code-header">impl<T> <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/any/trait.Any.html" title="trait core::any::Any">Any</a> for T<div class="where">where
|
||
T: 'static + ?<a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.type_id" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.93.1/src/core/any.rs.html#139">Source</a><a href="#method.type_id" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.93.1/core/any/trait.Any.html#tymethod.type_id" class="fn">type_id</a>(&self) -> <a class="struct" href="https://doc.rust-lang.org/1.93.1/core/any/struct.TypeId.html" title="struct core::any::TypeId">TypeId</a></h4></section></summary><div class='docblock'>Gets the <code>TypeId</code> of <code>self</code>. <a href="https://doc.rust-lang.org/1.93.1/core/any/trait.Any.html#tymethod.type_id">Read more</a></div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-Borrow%3CT%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.93.1/src/core/borrow.rs.html#212">Source</a><a href="#impl-Borrow%3CT%3E-for-T" class="anchor">§</a><h3 class="code-header">impl<T> <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/borrow/trait.Borrow.html" title="trait core::borrow::Borrow">Borrow</a><T> for T<div class="where">where
|
||
T: ?<a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.borrow" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.93.1/src/core/borrow.rs.html#214">Source</a><a href="#method.borrow" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.93.1/core/borrow/trait.Borrow.html#tymethod.borrow" class="fn">borrow</a>(&self) -> <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.reference.html">&T</a></h4></section></summary><div class='docblock'>Immutably borrows from an owned value. <a href="https://doc.rust-lang.org/1.93.1/core/borrow/trait.Borrow.html#tymethod.borrow">Read more</a></div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-BorrowMut%3CT%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.93.1/src/core/borrow.rs.html#221">Source</a><a href="#impl-BorrowMut%3CT%3E-for-T" class="anchor">§</a><h3 class="code-header">impl<T> <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/borrow/trait.BorrowMut.html" title="trait core::borrow::BorrowMut">BorrowMut</a><T> for T<div class="where">where
|
||
T: ?<a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</div></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.borrow_mut" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.93.1/src/core/borrow.rs.html#222">Source</a><a href="#method.borrow_mut" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.93.1/core/borrow/trait.BorrowMut.html#tymethod.borrow_mut" class="fn">borrow_mut</a>(&mut self) -> <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.reference.html">&mut T</a></h4></section></summary><div class='docblock'>Mutably borrows from an owned value. <a href="https://doc.rust-lang.org/1.93.1/core/borrow/trait.BorrowMut.html#tymethod.borrow_mut">Read more</a></div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-From%3CT%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.93.1/src/core/convert/mod.rs.html#785">Source</a><a href="#impl-From%3CT%3E-for-T" class="anchor">§</a><h3 class="code-header">impl<T> <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/convert/trait.From.html" title="trait core::convert::From">From</a><T> for T</h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.from" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.93.1/src/core/convert/mod.rs.html#788">Source</a><a href="#method.from" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.93.1/core/convert/trait.From.html#tymethod.from" class="fn">from</a>(t: T) -> T</h4></section></summary><div class="docblock"><p>Returns the argument unchanged.</p>
|
||
</div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-Into%3CU%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.93.1/src/core/convert/mod.rs.html#767-769">Source</a><a href="#impl-Into%3CU%3E-for-T" class="anchor">§</a><h3 class="code-header">impl<T, U> <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/convert/trait.Into.html" title="trait core::convert::Into">Into</a><U> for T<div class="where">where
|
||
U: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/convert/trait.From.html" title="trait core::convert::From">From</a><T>,</div></h3></section></summary><div class="impl-items"><details class="toggle method-toggle" open><summary><section id="method.into" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.93.1/src/core/convert/mod.rs.html#777">Source</a><a href="#method.into" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.93.1/core/convert/trait.Into.html#tymethod.into" class="fn">into</a>(self) -> U</h4></section></summary><div class="docblock"><p>Calls <code>U::from(self)</code>.</p>
|
||
<p>That is, this conversion is whatever the implementation of
|
||
<code><a href="https://doc.rust-lang.org/1.93.1/core/convert/trait.From.html" title="trait core::convert::From">From</a><T> for U</code> chooses to do.</p>
|
||
</div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-TryFrom%3CU%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.93.1/src/core/convert/mod.rs.html#827-829">Source</a><a href="#impl-TryFrom%3CU%3E-for-T" class="anchor">§</a><h3 class="code-header">impl<T, U> <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a><U> for T<div class="where">where
|
||
U: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/convert/trait.Into.html" title="trait core::convert::Into">Into</a><T>,</div></h3></section></summary><div class="impl-items"><details class="toggle" open><summary><section id="associatedtype.Error-1" class="associatedtype trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.93.1/src/core/convert/mod.rs.html#831">Source</a><a href="#associatedtype.Error-1" class="anchor">§</a><h4 class="code-header">type <a href="https://doc.rust-lang.org/1.93.1/core/convert/trait.TryFrom.html#associatedtype.Error" class="associatedtype">Error</a> = <a class="enum" href="https://doc.rust-lang.org/1.93.1/core/convert/enum.Infallible.html" title="enum core::convert::Infallible">Infallible</a></h4></section></summary><div class='docblock'>The type returned in the event of a conversion error.</div></details><details class="toggle method-toggle" open><summary><section id="method.try_from" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.93.1/src/core/convert/mod.rs.html#834">Source</a><a href="#method.try_from" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.93.1/core/convert/trait.TryFrom.html#tymethod.try_from" class="fn">try_from</a>(value: U) -> <a class="enum" href="https://doc.rust-lang.org/1.93.1/core/result/enum.Result.html" title="enum core::result::Result">Result</a><T, <T as <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a><U>>::<a class="associatedtype" href="https://doc.rust-lang.org/1.93.1/core/convert/trait.TryFrom.html#associatedtype.Error" title="type core::convert::TryFrom::Error">Error</a>></h4></section></summary><div class='docblock'>Performs the conversion.</div></details></div></details><details class="toggle implementors-toggle"><summary><section id="impl-TryInto%3CU%3E-for-T" class="impl"><a class="src rightside" href="https://doc.rust-lang.org/1.93.1/src/core/convert/mod.rs.html#811-813">Source</a><a href="#impl-TryInto%3CU%3E-for-T" class="anchor">§</a><h3 class="code-header">impl<T, U> <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/convert/trait.TryInto.html" title="trait core::convert::TryInto">TryInto</a><U> for T<div class="where">where
|
||
U: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a><T>,</div></h3></section></summary><div class="impl-items"><details class="toggle" open><summary><section id="associatedtype.Error" class="associatedtype trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.93.1/src/core/convert/mod.rs.html#815">Source</a><a href="#associatedtype.Error" class="anchor">§</a><h4 class="code-header">type <a href="https://doc.rust-lang.org/1.93.1/core/convert/trait.TryInto.html#associatedtype.Error" class="associatedtype">Error</a> = <U as <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a><T>>::<a class="associatedtype" href="https://doc.rust-lang.org/1.93.1/core/convert/trait.TryFrom.html#associatedtype.Error" title="type core::convert::TryFrom::Error">Error</a></h4></section></summary><div class='docblock'>The type returned in the event of a conversion error.</div></details><details class="toggle method-toggle" open><summary><section id="method.try_into" class="method trait-impl"><a class="src rightside" href="https://doc.rust-lang.org/1.93.1/src/core/convert/mod.rs.html#818">Source</a><a href="#method.try_into" class="anchor">§</a><h4 class="code-header">fn <a href="https://doc.rust-lang.org/1.93.1/core/convert/trait.TryInto.html#tymethod.try_into" class="fn">try_into</a>(self) -> <a class="enum" href="https://doc.rust-lang.org/1.93.1/core/result/enum.Result.html" title="enum core::result::Result">Result</a><U, <U as <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a><T>>::<a class="associatedtype" href="https://doc.rust-lang.org/1.93.1/core/convert/trait.TryFrom.html#associatedtype.Error" title="type core::convert::TryFrom::Error">Error</a>></h4></section></summary><div class='docblock'>Performs the conversion.</div></details></div></details></div></section></div></main></body></html> |