<!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 `alacritty`."><meta name="keywords" content="rust, rustlang, rust-lang, spawn"><title>alacritty::util::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><noscript><link rel="stylesheet" href="../../../noscript.css"></noscript><link rel="shortcut icon" href="../../../favicon.ico"><style type="text/css">#crate-search{background-image:url("../../../down-arrow.svg");}</style></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">☰</div><a href='../../../alacritty/index.html'><img src='../../../rust-logo.png' alt='logo' width='100'></a><div class="sidebar-elems"><p class='location'><a href='../../index.html'>alacritty</a>::<wbr><a href='../index.html'>util</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"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><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='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'>−</span>]</a></span><a class='srclink' href='https://doc.rust-lang.org/nightly/src/std/thread/mod.rs.html#607-611' title='goto source code'>[src]</a></span><span class='in-band'>Function <a href='../../index.html'>alacritty</a>::<wbr><a href='../index.html'>util</a>::<wbr><a href='index.html'>thread</a>::<wbr><a class="fn" href=''>spawn</a></span></h1><pre class='rust fn'>pub fn spawn<F, T>(f: F) -> <a class="struct" href="../../../alacritty/util/thread/struct.JoinHandle.html" title="struct alacritty::util::thread::JoinHandle">JoinHandle</a><T> <span class="where fmt-newline">where<br> F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.FnOnce.html" title="trait core::ops::function::FnOnce">FnOnce</a>() -> T + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + 'static,<br> T: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + 'static, </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> <div class="example-wrap"><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>();</pre></div> <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> <div class="example-wrap"><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">"Hello, thread"</span>.<span class="ident">to_owned</span>()) .<span class="ident">expect</span>(<span class="string">"Unable to send on channel"</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">"Unable to receive from channel"</span>); <span class="macro">println</span><span class="macro">!</span>(<span class="string">"{}"</span>, <span class="ident">value</span>); }); <span class="ident">sender</span>.<span class="ident">join</span>().<span class="ident">expect</span>(<span class="string">"The sender thread has panicked"</span>); <span class="ident">receiver</span>.<span class="ident">join</span>().<span class="ident">expect</span>(<span class="string">"The receiver thread has panicked"</span>);</pre></div> <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> <div class="example-wrap"><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">"{}"</span>, <span class="ident">result</span>);</pre></div> </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>⏎</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 = "alacritty";</script><script src="../../../aliases.js"></script><script src="../../../main.js"></script><script defer src="../../../search-index.js"></script></body></html>