blob: 24e9da4614ecaee5a3861ce61f91c90088b28b62 [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="Data-parallelism library that makes it easy to convert sequential computations into parallel"><meta name="keywords" content="rust, rustlang, rust-lang, rayon"><title>rayon - 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="../crates.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 mod crate"><!--[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="../rayon/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="../rayon/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">Crate rayon</a></h2><div class="sidebar-elems"><ul class="block"><li class="version">Version 1.7.0</li><li><a id="all-types" href="all.html">All Items</a></li></ul><section><ul class="block"><li><a href="#modules">Modules</a></li><li><a href="#structs">Structs</a></li><li><a href="#enums">Enums</a></li><li><a href="#functions">Functions</a></li></ul></section></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">Crate <a class="mod" href="#">rayon</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/rayon/lib.rs.html#1-160">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">&#x2212;</span>]</a></span></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Data-parallelism library that makes it easy to convert sequential
computations into parallel</p>
<p>Rayon is lightweight and convenient for introducing parallelism into existing
code. It guarantees data-race free executions and takes advantage of
parallelism when sensible, based on work-load at runtime.</p>
<h2 id="how-to-use-rayon"><a href="#how-to-use-rayon">How to use Rayon</a></h2>
<p>There are two ways to use Rayon:</p>
<ul>
<li><strong>High-level parallel constructs</strong> are the simplest way to use Rayon and also
typically the most efficient.
<ul>
<li><a href="iter/index.html">Parallel iterators</a> make it easy to convert a sequential iterator to
execute in parallel.
<ul>
<li>The <a href="iter/trait.ParallelIterator.html"><code>ParallelIterator</code></a> trait defines general methods for all parallel iterators.</li>
<li>The <a href="iter/trait.IndexedParallelIterator.html"><code>IndexedParallelIterator</code></a> trait adds methods for iterators that support random
access.</li>
</ul>
</li>
<li>The <a href="slice/trait.ParallelSliceMut.html#method.par_sort"><code>par_sort</code></a> method sorts <code>&amp;mut [T]</code> slices (or vectors) in parallel.</li>
<li><a href="iter/trait.ParallelExtend.html#tymethod.par_extend"><code>par_extend</code></a> can be used to efficiently grow collections with items produced
by a parallel iterator.</li>
</ul>
</li>
<li><strong>Custom tasks</strong> let you divide your work into parallel tasks yourself.
<ul>
<li><a href="fn.join.html"><code>join</code></a> is used to subdivide a task into two pieces.</li>
<li><a href="fn.scope.html"><code>scope</code></a> creates a scope within which you can create any number of parallel tasks.</li>
<li><a href="struct.ThreadPoolBuilder.html"><code>ThreadPoolBuilder</code></a> can be used to create your own thread pools or customize
the global one.</li>
</ul>
</li>
</ul>
<h2 id="basic-usage-and-the-rayon-prelude"><a href="#basic-usage-and-the-rayon-prelude">Basic usage and the Rayon prelude</a></h2>
<p>First, you will need to add <code>rayon</code> to your <code>Cargo.toml</code>.</p>
<p>Next, to use parallel iterators or the other high-level methods,
you need to import several traits. Those traits are bundled into
the module <a href="prelude/index.html"><code>rayon::prelude</code></a>. It is recommended that you import
all of these traits at once by adding <code>use rayon::prelude::*</code> at
the top of each module that uses Rayon methods.</p>
<p>These traits give you access to the <code>par_iter</code> method which provides
parallel implementations of many iterative functions such as <a href="iter/trait.ParallelIterator.html#method.map"><code>map</code></a>,
<a href="iter/trait.ParallelIterator.html#method.for_each"><code>for_each</code></a>, <a href="iter/trait.ParallelIterator.html#method.filter"><code>filter</code></a>, <a href="iter/trait.ParallelIterator.html#method.fold"><code>fold</code></a>, and <a href="iter/trait.ParallelIterator.html#provided-methods">more</a>.</p>
<h2 id="crate-layout"><a href="#crate-layout">Crate Layout</a></h2>
<p>Rayon extends many of the types found in the standard library with
parallel iterator implementations. The modules in the <code>rayon</code>
crate mirror <a href="https://doc.rust-lang.org/std/"><code>std</code></a> itself: so, e.g., the <code>option</code> module in
Rayon contains parallel iterators for the <code>Option</code> type, which is
found in <a href="https://doc.rust-lang.org/std/option/index.html">the <code>option</code> module of <code>std</code></a>. Similarly, the
<code>collections</code> module in Rayon offers parallel iterator types for
<a href="https://doc.rust-lang.org/std/collections/index.html">the <code>collections</code> from <code>std</code></a>. You will rarely need to access
these submodules unless you need to name iterator types
explicitly.</p>
<h2 id="targets-without-threading"><a href="#targets-without-threading">Targets without threading</a></h2>
<p>Rayon has limited support for targets without <code>std</code> threading implementations.
See the <a href="../rayon_core/index.html" title="rayon_core"><code>rayon_core</code></a> documentation for more information about its global fallback.</p>
<h2 id="other-questions"><a href="#other-questions">Other questions?</a></h2>
<p>See <a href="https://github.com/rayon-rs/rayon/blob/master/FAQ.md">the Rayon FAQ</a>.</p>
</div></details><h2 id="modules" class="small-section-header"><a href="#modules">Modules</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="mod" href="array/index.html" title="rayon::array mod">array</a></div><div class="item-right docblock-short">Parallel iterator types for <a href="https://doc.rust-lang.org/std/primitive.array.html">arrays</a> (<code>[T; N]</code>)</div></div><div class="item-row"><div class="item-left module-item"><a class="mod" href="collections/index.html" title="rayon::collections mod">collections</a></div><div class="item-right docblock-short">Parallel iterator types for <a href="https://doc.rust-lang.org/stable/std/collections/">standard collections</a></div></div><div class="item-row"><div class="item-left module-item"><a class="mod" href="iter/index.html" title="rayon::iter mod">iter</a></div><div class="item-right docblock-short">Traits for writing parallel programs using an iterator-style interface</div></div><div class="item-row"><div class="item-left module-item"><a class="mod" href="option/index.html" title="rayon::option mod">option</a></div><div class="item-right docblock-short">Parallel iterator types for <a href="https://doc.rust-lang.org/stable/std/option/">options</a></div></div><div class="item-row"><div class="item-left module-item"><a class="mod" href="prelude/index.html" title="rayon::prelude mod">prelude</a></div><div class="item-right docblock-short">The rayon prelude imports the various <code>ParallelIterator</code> traits.
The intention is that one can include <code>use rayon::prelude::*</code> and
have easy access to the various traits and methods you will need.</div></div><div class="item-row"><div class="item-left module-item"><a class="mod" href="range/index.html" title="rayon::range mod">range</a></div><div class="item-right docblock-short">Parallel iterator types for <a href="https://doc.rust-lang.org/core/ops/struct.Range.html">ranges</a>,
the type for values created by <code>a..b</code> expressions</div></div><div class="item-row"><div class="item-left module-item"><a class="mod" href="range_inclusive/index.html" title="rayon::range_inclusive mod">range_inclusive</a></div><div class="item-right docblock-short">Parallel iterator types for <a href="https://doc.rust-lang.org/core/ops/struct.RangeInclusive.html">inclusive ranges</a>,
the type for values created by <code>a..=b</code> expressions</div></div><div class="item-row"><div class="item-left module-item"><a class="mod" href="result/index.html" title="rayon::result mod">result</a></div><div class="item-right docblock-short">Parallel iterator types for <a href="https://doc.rust-lang.org/stable/std/result/">results</a></div></div><div class="item-row"><div class="item-left module-item"><a class="mod" href="slice/index.html" title="rayon::slice mod">slice</a></div><div class="item-right docblock-short">Parallel iterator types for <a href="https://doc.rust-lang.org/stable/std/slice/">slices</a></div></div><div class="item-row"><div class="item-left module-item"><a class="mod" href="str/index.html" title="rayon::str mod">str</a></div><div class="item-right docblock-short">Parallel iterator types for <a href="https://doc.rust-lang.org/stable/std/str/">strings</a></div></div><div class="item-row"><div class="item-left module-item"><a class="mod" href="string/index.html" title="rayon::string mod">string</a></div><div class="item-right docblock-short">This module contains the parallel iterator types for owned strings
(<code>String</code>). You will rarely need to interact with it directly
unless you have need to name one of the iterator types.</div></div><div class="item-row"><div class="item-left module-item"><a class="mod" href="vec/index.html" title="rayon::vec mod">vec</a></div><div class="item-right docblock-short">Parallel iterator types for <a href="https://doc.rust-lang.org/stable/std/vec/">vectors</a> (<code>Vec&lt;T&gt;</code>)</div></div></div><h2 id="structs" class="small-section-header"><a href="#structs">Structs</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.BroadcastContext.html" title="rayon::BroadcastContext struct">BroadcastContext</a></div><div class="item-right docblock-short">Provides context to a closure called by <code>broadcast</code>.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.FnContext.html" title="rayon::FnContext struct">FnContext</a></div><div class="item-right docblock-short">Provides the calling context to a closure called by <code>join_context</code>.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Scope.html" title="rayon::Scope struct">Scope</a></div><div class="item-right docblock-short">Represents a fork-join scope which can be used to spawn any number of tasks.
See <a href="fn.scope.html"><code>scope()</code></a> for more information.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.ScopeFifo.html" title="rayon::ScopeFifo struct">ScopeFifo</a></div><div class="item-right docblock-short">Represents a fork-join scope which can be used to spawn any number of tasks.
Those spawned from the same thread are prioritized in relative FIFO order.
See <a href="fn.scope_fifo.html"><code>scope_fifo()</code></a> for more information.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.ThreadBuilder.html" title="rayon::ThreadBuilder struct">ThreadBuilder</a></div><div class="item-right docblock-short">Thread builder used for customization via
<a href="struct.ThreadPoolBuilder.html#method.spawn_handler"><code>ThreadPoolBuilder::spawn_handler</code></a>.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.ThreadPool.html" title="rayon::ThreadPool struct">ThreadPool</a></div><div class="item-right docblock-short">Represents a user created <a href="https://en.wikipedia.org/wiki/Thread_pool">thread-pool</a>.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.ThreadPoolBuildError.html" title="rayon::ThreadPoolBuildError struct">ThreadPoolBuildError</a></div><div class="item-right docblock-short">Error when initializing a thread pool.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.ThreadPoolBuilder.html" title="rayon::ThreadPoolBuilder struct">ThreadPoolBuilder</a></div><div class="item-right docblock-short">Used to create a new <a href="struct.ThreadPool.html"><code>ThreadPool</code></a> or to configure the global rayon thread pool.</div></div></div><h2 id="enums" class="small-section-header"><a href="#enums">Enums</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="enum" href="enum.Yield.html" title="rayon::Yield enum">Yield</a></div><div class="item-right docblock-short">Result of <a href="fn.yield_now.html" title="yield_now()"><code>yield_now()</code></a> or <a href="fn.yield_local.html" title="yield_local()"><code>yield_local()</code></a>.</div></div></div><h2 id="functions" class="small-section-header"><a href="#functions">Functions</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.broadcast.html" title="rayon::broadcast fn">broadcast</a></div><div class="item-right docblock-short">Executes <code>op</code> within every thread in the current threadpool. If this is
called from a non-Rayon thread, it will execute in the global threadpool.
Any attempts to use <code>join</code>, <code>scope</code>, or parallel iterators will then operate
within that threadpool. When the call has completed on each thread, returns
a vector containing all of their return values.</div></div><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.current_num_threads.html" title="rayon::current_num_threads fn">current_num_threads</a></div><div class="item-right docblock-short">Returns the number of threads in the current registry. If this
code is executing within a Rayon thread-pool, then this will be
the number of threads for the thread-pool of the current
thread. Otherwise, it will be the number of threads for the global
thread-pool.</div></div><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.current_thread_index.html" title="rayon::current_thread_index fn">current_thread_index</a></div><div class="item-right docblock-short">If called from a Rayon worker thread, returns the index of that
thread within its current pool; if not called from a Rayon thread,
returns <code>None</code>.</div></div><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.in_place_scope.html" title="rayon::in_place_scope fn">in_place_scope</a></div><div class="item-right docblock-short">Creates a “fork-join” scope <code>s</code> and invokes the closure with a
reference to <code>s</code>. This closure can then spawn asynchronous tasks
into <code>s</code>. Those tasks may run asynchronously with respect to the
closure; they may themselves spawn additional tasks into <code>s</code>. When
the closure returns, it will block until all tasks that have been
spawned into <code>s</code> complete.</div></div><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.in_place_scope_fifo.html" title="rayon::in_place_scope_fifo fn">in_place_scope_fifo</a></div><div class="item-right docblock-short">Creates a “fork-join” scope <code>s</code> with FIFO order, and invokes the
closure with a reference to <code>s</code>. This closure can then spawn
asynchronous tasks into <code>s</code>. Those tasks may run asynchronously with
respect to the closure; they may themselves spawn additional tasks
into <code>s</code>. When the closure returns, it will block until all tasks
that have been spawned into <code>s</code> complete.</div></div><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.join.html" title="rayon::join fn">join</a></div><div class="item-right docblock-short">Takes two closures and <em>potentially</em> runs them in parallel. It
returns a pair of the results from those closures.</div></div><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.join_context.html" title="rayon::join_context fn">join_context</a></div><div class="item-right docblock-short">Identical to <code>join</code>, except that the closures have a parameter
that provides context for the way the closure has been called,
especially indicating whether they’re executing on a different
thread than where <code>join_context</code> was called. This will occur if
the second job is stolen by a different thread, or if
<code>join_context</code> was called from outside the thread pool to begin
with.</div></div><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.max_num_threads.html" title="rayon::max_num_threads fn">max_num_threads</a></div><div class="item-right docblock-short">Returns the maximum number of threads that Rayon supports in a single thread-pool.</div></div><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.scope.html" title="rayon::scope fn">scope</a></div><div class="item-right docblock-short">Creates a “fork-join” scope <code>s</code> and invokes the closure with a
reference to <code>s</code>. This closure can then spawn asynchronous tasks
into <code>s</code>. Those tasks may run asynchronously with respect to the
closure; they may themselves spawn additional tasks into <code>s</code>. When
the closure returns, it will block until all tasks that have been
spawned into <code>s</code> complete.</div></div><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.scope_fifo.html" title="rayon::scope_fifo fn">scope_fifo</a></div><div class="item-right docblock-short">Creates a “fork-join” scope <code>s</code> with FIFO order, and invokes the
closure with a reference to <code>s</code>. This closure can then spawn
asynchronous tasks into <code>s</code>. Those tasks may run asynchronously with
respect to the closure; they may themselves spawn additional tasks
into <code>s</code>. When the closure returns, it will block until all tasks
that have been spawned into <code>s</code> complete.</div></div><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.spawn.html" title="rayon::spawn fn">spawn</a></div><div class="item-right docblock-short">Fires off a task into the Rayon threadpool in the “static” or
“global” scope. Just like a standard thread, this task is not
tied to the current stack frame, and hence it cannot hold any
references other than those with <code>'static</code> lifetime. If you want
to spawn a task that references stack data, use <a href="fn.scope.html">the <code>scope()</code>
function</a> to create a scope.</div></div><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.spawn_broadcast.html" title="rayon::spawn_broadcast fn">spawn_broadcast</a></div><div class="item-right docblock-short">Spawns an asynchronous task on every thread in this thread-pool. This task
will run in the implicit, global scope, which means that it may outlast the
current stack frame – therefore, it cannot capture any references onto the
stack (you will likely need a <code>move</code> closure).</div></div><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.spawn_fifo.html" title="rayon::spawn_fifo fn">spawn_fifo</a></div><div class="item-right docblock-short">Fires off a task into the Rayon threadpool in the “static” or
“global” scope. Just like a standard thread, this task is not
tied to the current stack frame, and hence it cannot hold any
references other than those with <code>'static</code> lifetime. If you want
to spawn a task that references stack data, use <a href="fn.scope_fifo.html">the <code>scope_fifo()</code>
function</a> to create a scope.</div></div><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.yield_local.html" title="rayon::yield_local fn">yield_local</a></div><div class="item-right docblock-short">Cooperatively yields execution to local Rayon work.</div></div><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.yield_now.html" title="rayon::yield_now fn">yield_now</a></div><div class="item-right docblock-short">Cooperatively yields execution to Rayon.</div></div></div></section></div></main><div id="rustdoc-vars" data-root-path="../" data-current-crate="rayon" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html>