Sophie

Sophie

distrib > Mageia > 6 > armv7hl > media > core-updates > by-pkgid > 4e2dbb669434a7691662cb2f0ad38972 > files > 15075

rust-doc-1.28.0-1.mga6.armv7hl.rpm

<!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="API documentation for the Rust `spawn` fn in crate `std`."><meta name="keywords" content="rust, rustlang, rust-lang, spawn"><title>std::thread::spawn - Rust</title><link rel="stylesheet" type="text/css" href="../../normalize.css"><link rel="stylesheet" type="text/css" href="../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../../dark.css"><link rel="stylesheet" type="text/css" href="../../light.css" id="themeStyle"><script src="../../storage.js"></script><link rel="shortcut icon" href="https://doc.rust-lang.org/favicon.ico"></head><body class="rustdoc fn"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu">&#9776;</div><a href='../../std/index.html'><img src='https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png' alt='logo' width='100'></a><div class="sidebar-elems"><p class='location'><a href='../index.html'>std</a>::<wbr><a href='index.html'>thread</a></p><script>window.sidebarCurrent = {name: 'spawn', ty: 'fn', relpath: ''};</script><script defer src="sidebar-items.js"></script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!"><img src="../../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices"></div></div><script src="../../theme.js"></script><nav class="sub"><form class="search-form js-only"><div class="search-container"><input class="search-input" name="search" autocomplete="off" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><a id="settings-menu" href="../../settings.html"><img src="../../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><span class='in-band'>Function <a href='../index.html'>std</a>::<wbr><a href='index.html'>thread</a>::<wbr><a class="fn" href=''>spawn</a></span><span class='out-of-band'><span class='since' title='Stable since Rust version 1.0.0'>1.0.0</span><span id='render-detail'><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class='inner'>&#x2212;</span>]</a></span><a class='srclink' href='../../src/std/thread/mod.rs.html#536-540' title='goto source code'>[src]</a></span></h1><pre class='rust fn'>pub fn spawn&lt;F, T&gt;(f: F) -&gt; <a class="struct" href="../../std/thread/struct.JoinHandle.html" title="struct std::thread::JoinHandle">JoinHandle</a>&lt;T&gt; <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;F: <a class="trait" href="../../std/ops/trait.FnOnce.html" title="trait std::ops::FnOnce">FnOnce</a>() -&gt; T,<br>&nbsp;&nbsp;&nbsp;&nbsp;F: <a class="trait" href="../../std/marker/trait.Send.html" title="trait std::marker::Send">Send</a> + 'static,<br>&nbsp;&nbsp;&nbsp;&nbsp;T: <a class="trait" href="../../std/marker/trait.Send.html" title="trait std::marker::Send">Send</a> + 'static,&nbsp;</span></pre><div class='docblock'><p>Spawns a new thread, returning a <a href="../../std/thread/struct.JoinHandle.html"><code>JoinHandle</code></a> for it.</p>
<p>The join handle will implicitly <em>detach</em> the child thread upon being
dropped. In this case, the child thread may outlive the parent (unless
the parent thread is the main thread; the whole process is terminated when
the main thread finishes). Additionally, the join handle provides a <a href="../../std/thread/struct.JoinHandle.html#method.join"><code>join</code></a>
method that can be used to join the child thread. If the child thread
panics, <a href="../../std/thread/struct.JoinHandle.html#method.join"><code>join</code></a> will return an <a href="../../std/result/enum.Result.html#variant.Err"><code>Err</code></a> containing the argument given to
<a href="../../std/macro.panic.html"><code>panic</code></a>.</p>
<p>This will create a thread using default parameters of <a href="../../std/thread/struct.Builder.html"><code>Builder</code></a>, if you
want to specify the stack size or the name of the thread, use this API
instead.</p>
<p>As you can see in the signature of <code>spawn</code> there are two constraints on
both the closure given to <code>spawn</code> and its return value, let's explain them:</p>
<ul>
<li>The <code>'static</code> constraint means that the closure and its return value
must have a lifetime of the whole program execution. The reason for this
is that threads can <code>detach</code> and outlive the lifetime they have been
created in.
Indeed if the thread, and by extension its return value, can outlive their
caller, we need to make sure that they will be valid afterwards, and since
we <em>can't</em> know when it will return we need to have them valid as long as
possible, that is until the end of the program, hence the <code>'static</code>
lifetime.</li>
<li>The <a href="../../std/marker/trait.Send.html"><code>Send</code></a> constraint is because the closure will need to be passed
<em>by value</em> from the thread where it is spawned to the new thread. Its
return value will need to be passed from the new thread to the thread
where it is <code>join</code>ed.
As a reminder, the <a href="../../std/marker/trait.Send.html"><code>Send</code></a> marker trait expresses that it is safe to be
passed from thread to thread. <a href="../../std/marker/trait.Sync.html"><code>Sync</code></a> expresses that it is safe to have a
reference be passed from thread to thread.</li>
</ul>
<h1 id="panics" class="section-header"><a href="#panics">Panics</a></h1>
<p>Panics if the OS fails to create a thread; use <a href="../../std/thread/struct.Builder.html#method.spawn"><code>Builder::spawn</code></a>
to recover from such errors.</p>
<h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1>
<p>Creating a thread.</p>

<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">thread</span>;

<span class="kw">let</span> <span class="ident">handler</span> <span class="op">=</span> <span class="ident">thread</span>::<span class="ident">spawn</span>(<span class="op">||</span> {
    <span class="comment">// thread code</span>
});

<span class="ident">handler</span>.<span class="ident">join</span>().<span class="ident">unwrap</span>();<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Athread%3B%0A%0Alet%20handler%20%3D%20thread%3A%3Aspawn(%7C%7C%20%7B%0A%20%20%20%20%2F%2F%20thread%20code%0A%7D)%3B%0A%0Ahandler.join().unwrap()%3B%0A%7D">Run</a></pre>
<p>As mentioned in the module documentation, threads are usually made to
communicate using <a href="../../std/sync/mpsc/index.html"><code>channels</code></a>, here is how it usually looks.</p>
<p>This example also shows how to use <code>move</code>, in order to give ownership
of values to a thread.</p>

<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">thread</span>;
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">sync</span>::<span class="ident">mpsc</span>::<span class="ident">channel</span>;

<span class="kw">let</span> (<span class="ident">tx</span>, <span class="ident">rx</span>) <span class="op">=</span> <span class="ident">channel</span>();

<span class="kw">let</span> <span class="ident">sender</span> <span class="op">=</span> <span class="ident">thread</span>::<span class="ident">spawn</span>(<span class="kw">move</span> <span class="op">||</span> {
    <span class="ident">tx</span>.<span class="ident">send</span>(<span class="string">&quot;Hello, thread&quot;</span>.<span class="ident">to_owned</span>())
        .<span class="ident">expect</span>(<span class="string">&quot;Unable to send on channel&quot;</span>);
});

<span class="kw">let</span> <span class="ident">receiver</span> <span class="op">=</span> <span class="ident">thread</span>::<span class="ident">spawn</span>(<span class="kw">move</span> <span class="op">||</span> {
    <span class="kw">let</span> <span class="ident">value</span> <span class="op">=</span> <span class="ident">rx</span>.<span class="ident">recv</span>().<span class="ident">expect</span>(<span class="string">&quot;Unable to receive from channel&quot;</span>);
    <span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;{}&quot;</span>, <span class="ident">value</span>);
});

<span class="ident">sender</span>.<span class="ident">join</span>().<span class="ident">expect</span>(<span class="string">&quot;The sender thread has panicked&quot;</span>);
<span class="ident">receiver</span>.<span class="ident">join</span>().<span class="ident">expect</span>(<span class="string">&quot;The receiver thread has panicked&quot;</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Athread%3B%0Ause%20std%3A%3Async%3A%3Ampsc%3A%3Achannel%3B%0A%0Alet%20(tx%2C%20rx)%20%3D%20channel()%3B%0A%0Alet%20sender%20%3D%20thread%3A%3Aspawn(move%20%7C%7C%20%7B%0A%20%20%20%20tx.send(%22Hello%2C%20thread%22.to_owned())%0A%20%20%20%20%20%20%20%20.expect(%22Unable%20to%20send%20on%20channel%22)%3B%0A%7D)%3B%0A%0Alet%20receiver%20%3D%20thread%3A%3Aspawn(move%20%7C%7C%20%7B%0A%20%20%20%20let%20value%20%3D%20rx.recv().expect(%22Unable%20to%20receive%20from%20channel%22)%3B%0A%20%20%20%20println!(%22%7B%7D%22%2C%20value)%3B%0A%7D)%3B%0A%0Asender.join().expect(%22The%20sender%20thread%20has%20panicked%22)%3B%0Areceiver.join().expect(%22The%20receiver%20thread%20has%20panicked%22)%3B%0A%7D">Run</a></pre>
<p>A thread can also return a value through its <a href="../../std/thread/struct.JoinHandle.html"><code>JoinHandle</code></a>, you can use
this to make asynchronous computations (futures might be more appropriate
though).</p>

<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">thread</span>;

<span class="kw">let</span> <span class="ident">computation</span> <span class="op">=</span> <span class="ident">thread</span>::<span class="ident">spawn</span>(<span class="op">||</span> {
    <span class="comment">// Some expensive computation.</span>
    <span class="number">42</span>
});

<span class="kw">let</span> <span class="ident">result</span> <span class="op">=</span> <span class="ident">computation</span>.<span class="ident">join</span>().<span class="ident">unwrap</span>();
<span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;{}&quot;</span>, <span class="ident">result</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Athread%3B%0A%0Alet%20computation%20%3D%20thread%3A%3Aspawn(%7C%7C%20%7B%0A%20%20%20%20%2F%2F%20Some%20expensive%20computation.%0A%20%20%20%2042%0A%7D)%3B%0A%0Alet%20result%20%3D%20computation.join().unwrap()%3B%0Aprintln!(%22%7B%7D%22%2C%20result)%3B%0A%7D">Run</a></pre>
</div></section><section id="search" class="content hidden"></section><section class="footer"></section><aside id="help" class="hidden"><div><h1 class="hidden">Help</h1><div class="shortcuts"><h2>Keyboard Shortcuts</h2><dl><dt><kbd>?</kbd></dt><dd>Show this help dialog</dd><dt><kbd>S</kbd></dt><dd>Focus the search field</dd><dt><kbd>↑</kbd></dt><dd>Move up in search results</dd><dt><kbd>↓</kbd></dt><dd>Move down in search results</dd><dt><kbd>↹</kbd></dt><dd>Switch tab</dd><dt><kbd>&#9166;</kbd></dt><dd>Go to active search result</dd><dt><kbd>+</kbd></dt><dd>Expand all sections</dd><dt><kbd>-</kbd></dt><dd>Collapse all sections</dd></dl></div><div class="infos"><h2>Search Tricks</h2><p>Prefix searches with a type followed by a colon (e.g. <code>fn:</code>) to restrict the search to a given type.</p><p>Accepted types are: <code>fn</code>, <code>mod</code>, <code>struct</code>, <code>enum</code>, <code>trait</code>, <code>type</code>, <code>macro</code>, and <code>const</code>.</p><p>Search functions by type signature (e.g. <code>vec -> usize</code> or <code>* -> vec</code>)</p><p>Search multiple things at once by splitting your query with comma (e.g. <code>str,u8</code> or <code>String,struct:Vec,test</code>)</p></div></div></aside><script>window.rootPath = "../../";window.currentCrate = "std";</script><script src="../../aliases.js"></script><script src="../../main.js"></script><script defer src="../../search-index.js"></script></body></html>