You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Trac3r-rust/doc/rand/seq/trait.SliceRandom.html

96 lines
50 KiB

5 years ago
<!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 `SliceRandom` trait in crate `rand`."><meta name="keywords" content="rust, rustlang, rust-lang, SliceRandom"><title>rand::seq::SliceRandom - 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="https://www.rust-lang.org/favicon.ico"><style type="text/css">#crate-search{background-image:url("../../down-arrow.svg");}</style></head><body class="rustdoc trait"><!--[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='../../rand/index.html'><div class='logo-container'><img src='https://www.rust-lang.org/logos/rust-logo-128x128-blk.png' alt='logo'></div></a><p class='location'>Trait SliceRandom</p><div class="sidebar-elems"><div class="block items"><a class="sidebar-title" href="#associated-types">Associated Types</a><div class="sidebar-links"><a href="#associatedtype.Item">Item</a></div><a class="sidebar-title" href="#required-methods">Required Methods</a><div class="sidebar-links"><a href="#tymethod.choose">choose</a><a href="#tymethod.choose_multiple">choose_multiple</a><a href="#tymethod.choose_mut">choose_mut</a><a href="#tymethod.choose_weighted">choose_weighted</a><a href="#tymethod.choose_weighted_mut">choose_weighted_mut</a><a href="#tymethod.partial_shuffle">partial_shuffle</a><a href="#tymethod.shuffle">shuffle</a></div><a class="sidebar-title" href="#foreign-impls">Implementations on Foreign Types</a><div class="sidebar-links"><a href="#impl-SliceRandom-for-%5BT%5D">[T]</a></div><a class="sidebar-title" href="#implementors">Implementors</a></div><p class='location'><a href='../index.html'>rand</a>::<wbr><a href='index.html'>seq</a></p><script>window.sidebarCurrent = {name: 'SliceRandom', ty: 'trait', 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 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/rand/seq/mod.rs.html#28-170' title='goto source code'>[src]</a></span><span class='in-band'>Trait <a href='../index.html'>rand</a>::<wbr><a href='index.html'>seq</a>::<wbr><a class="trait" href=''>SliceRandom</a></span></h1><div class="docblock type-decl hidden-by-usual-hider"><pre class='rust trait'>pub trait SliceRandom {
type <a href='#associatedtype.Item' class="type">Item</a>;
fn <a href='#tymethod.choose' class='fnname'>choose</a>&lt;R:&nbsp;?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>&gt;(&amp;self, rng: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;mut </a>R) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;&amp;Self::<a class="type" href="../../rand/seq/trait.SliceRandom.html#associatedtype.Item" title="type rand::seq::SliceRandom::Item">Item</a>&gt;<br>&nbsp;&nbsp;&nbsp; <span class="where">where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;R: <a class="trait" href="../../rand/trait.Rng.html" title="trait rand::Rng">Rng</a></span>;
<div class='item-spacer'></div> fn <a href='#tymethod.choose_mut' class='fnname'>choose_mut</a>&lt;R:&nbsp;?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>&gt;(&amp;mut self, rng: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;mut </a>R) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;&amp;mut Self::<a class="type" href="../../rand/seq/trait.SliceRandom.html#associatedtype.Item" title="type rand::seq::SliceRandom::Item">Item</a>&gt;<br>&nbsp;&nbsp;&nbsp; <span class="where">where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;R: <a class="trait" href="../../rand/trait.Rng.html" title="trait rand::Rng">Rng</a></span>;
<div class='item-spacer'></div> fn <a href='#tymethod.choose_multiple' class='fnname'>choose_multiple</a>&lt;R:&nbsp;?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>&gt;(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;self, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rng: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;mut </a>R, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;amount: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a><br>&nbsp;&nbsp;&nbsp;&nbsp;) -&gt; <a class="struct" href="../../rand/seq/struct.SliceChooseIter.html" title="struct rand::seq::SliceChooseIter">SliceChooseIter</a>&lt;Self, Self::<a class="type" href="../../rand/seq/trait.SliceRandom.html#associatedtype.Item" title="type rand::seq::SliceRandom::Item">Item</a>&gt;<br>&nbsp;&nbsp;&nbsp; <span class="where">where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;R: <a class="trait" href="../../rand/trait.Rng.html" title="trait rand::Rng">Rng</a></span>;
<div class='item-spacer'></div> fn <a href='#tymethod.choose_weighted' class='fnname'>choose_weighted</a>&lt;R:&nbsp;?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>, F, B, X&gt;(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;self, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rng: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;mut </a>R, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;weight: F<br>&nbsp;&nbsp;&nbsp;&nbsp;) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;&amp;Self::<a class="type" href="../../rand/seq/trait.SliceRandom.html#associatedtype.Item" title="type rand::seq::SliceRandom::Item">Item</a>, <a class="enum" href="../../rand/distributions/enum.WeightedError.html" title="enum rand::distributions::WeightedError">WeightedError</a>&gt;<br>&nbsp;&nbsp;&nbsp; <span class="where">where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;R: <a class="trait" href="../../rand/trait.Rng.html" title="trait rand::Rng">Rng</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.Fn.html" title="trait core::ops::function::Fn">Fn</a>(&amp;Self::<a class="type" href="../../rand/seq/trait.SliceRandom.html#associatedtype.Item" title="type rand::seq::SliceRandom::Item">Item</a>) -&gt; B,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;B: <a class="trait" href="../../rand/distributions/uniform/trait.SampleBorrow.html" title="trait rand::distributions::uniform::SampleBorrow">SampleBorrow</a>&lt;X&gt;,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;X: <a class="trait" href="../../rand/distributions/uniform/trait.SampleUniform.html" title="trait rand::distributions::uniform::SampleUniform">SampleUniform</a> + for&lt;'a&gt; <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/arith/trait.AddAssign.html" title="trait core::ops::arith::AddAssign">AddAssign</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;'a </a>X&gt; + <a class="trait" href="https://doc.rust-lang.org/nightly/core/cmp/trait.PartialOrd.html" title="trait core::cmp::PartialOrd">PartialOrd</a>&lt;X&gt; + <a class="trait" href="https://doc.rust-lang.org/nightly/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/default/trait.Default.html" title="trait core::default::Default">Default</a></span>;
<div class='item-spacer'></div> fn <a href='#tymethod.choose_weighted_mut' class='fnname'>choose_weighted_mut</a>&lt;R:&nbsp;?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>, F, B, X&gt;(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;mut self, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rng: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;mut </a>R, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;weight: F<br>&nbsp;&nbsp;&nbsp;&nbsp;) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;&amp;mut Self::<a class="type" href="../../rand/seq/trait.SliceRandom.html#associatedtype.Item" title="type rand::seq::SliceRandom::Item">Item</a>, <a class="enum" href="../../rand/distributions/enum.WeightedError.html" title="enum rand::distributions::WeightedError">WeightedError</a>&gt;<br>&nbsp;&nbsp;&nbsp; <span class="where">where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;R: <a class="trait" href="../../rand/trait.Rng.html" title="trait rand::Rng">Rng</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.Fn.html" title="trait core::ops::function::Fn">Fn</a>(&amp;Self::<a class="type" href="../../rand/seq/trait.SliceRandom.html#associatedtype.Item" title="type rand::seq::SliceRandom::Item">Item</a>) -&gt; B,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;B: <a class="trait" href="../../rand/distributions/uniform/trait.SampleBorrow.html" title="trait rand::distributions::uniform::SampleBorrow">SampleBorrow</a>&lt;X&gt;,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;X: <a class="trait" href="../../rand/distributions/uniform/trait.SampleUniform.html" title="trait rand::distributions::uniform::SampleUniform">SampleUniform</a> + for&lt;'a&gt; <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/arith/trait.AddAssign.html" title="trait core::ops::arith::AddAssign">AddAssign</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;'a </a>X&gt; + <a class="trait" href="https://doc.rust-lang.org/nightly/core/cmp/trait.PartialOrd.html" title="trait core::cmp::PartialOrd">PartialOrd</a>&lt;X&gt; + <a class="trait" href="https://doc.rust-lang.org/nightly/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/default/trait.Default.html" title="trait core::default::Default">Default</a></span>;
<div class='item-spacer'></div> fn <a href='#tymethod.shuffle' class='fnname'>shuffle</a>&lt;R:&nbsp;?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>&gt;(&amp;mut self, rng: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;mut </a>R)<br>&nbsp;&nbsp;&nbsp; <span class="where">where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;R: <a class="trait" href="../../rand/trait.Rng.html" title="trait rand::Rng">Rng</a></span>;
<div class='item-spacer'></div> fn <a href='#tymethod.partial_shuffle' class='fnname'>partial_shuffle</a>&lt;R:&nbsp;?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>&gt;(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;mut self, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rng: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;mut </a>R, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;amount: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a><br>&nbsp;&nbsp;&nbsp;&nbsp;) -&gt; <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.tuple.html">(</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">&amp;mut [</a>Self::<a class="type" href="../../rand/seq/trait.SliceRandom.html#associatedtype.Item" title="type rand::seq::SliceRandom::Item">Item</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">]</a>, <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">&amp;mut [</a>Self::<a class="type" href="../../rand/seq/trait.SliceRandom.html#associatedtype.Item" title="type rand::seq::SliceRandom::Item">Item</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">]</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.tuple.html">)</a><br>&nbsp;&nbsp;&nbsp; <span class="where">where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;R: <a class="trait" href="../../rand/trait.Rng.html" title="trait rand::Rng">Rng</a></span>;
}</pre></div><div class='docblock'><p>Extension trait on slices, providing random mutation and sampling methods.</p>
<p>An implementation is provided for slices. This may also be implementable for
other types.</p>
</div>
<h2 id='associated-types' class='small-section-header'>Associated Types<a href='#associated-types' class='anchor'></a></h2><div class='methods'><h3 id='associatedtype.Item' class='method'><code id='Item.t'>type <a href='#associatedtype.Item' class="type">Item</a></code></h3><div class='docblock'><p>The element type.</p>
</div></div><span class='loading-content'>Loading content...</span>
<h2 id='required-methods' class='small-section-header'>Required methods<a href='#required-methods' class='anchor'></a></h2><div class='methods'><h3 id='tymethod.choose' class='method'><code id='choose.v'>fn <a href='#tymethod.choose' class='fnname'>choose</a>&lt;R:&nbsp;?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>&gt;(&amp;self, rng: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;mut </a>R) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;&amp;Self::<a class="type" href="../../rand/seq/trait.SliceRandom.html#associatedtype.Item" title="type rand::seq::SliceRandom::Item">Item</a>&gt; <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;R: <a class="trait" href="../../rand/trait.Rng.html" title="trait rand::Rng">Rng</a>,&nbsp;</span></code></h3><div class='docblock'><p>Returns a reference to one random element of the slice, or <code>None</code> if the
slice is empty.</p>
<p>Depending on the implementation, complexity is expected to be <code>O(1)</code>.</p>
<h1 id="example" class="section-header"><a href="#example">Example</a></h1>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">rand</span>::<span class="ident">thread_rng</span>;
<span class="kw">use</span> <span class="ident">rand</span>::<span class="ident">seq</span>::<span class="ident">SliceRandom</span>;
<span class="kw">let</span> <span class="ident">choices</span> <span class="op">=</span> [<span class="number">1</span>, <span class="number">2</span>, <span class="number">4</span>, <span class="number">8</span>, <span class="number">16</span>, <span class="number">32</span>];
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">rng</span> <span class="op">=</span> <span class="ident">thread_rng</span>();
<span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;{:?}&quot;</span>, <span class="ident">choices</span>.<span class="ident">choose</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">rng</span>));
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">choices</span>[..<span class="number">0</span>].<span class="ident">choose</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">rng</span>), <span class="prelude-val">None</span>);</pre></div>
</div><h3 id='tymethod.choose_mut' class='method'><code id='choose_mut.v'>fn <a href='#tymethod.choose_mut' class='fnname'>choose_mut</a>&lt;R:&nbsp;?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>&gt;(&amp;mut self, rng: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;mut </a>R) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;&amp;mut Self::<a class="type" href="../../rand/seq/trait.SliceRandom.html#associatedtype.Item" title="type rand::seq::SliceRandom::Item">Item</a>&gt; <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;R: <a class="trait" href="../../rand/trait.Rng.html" title="trait rand::Rng">Rng</a>,&nbsp;</span></code></h3><div class='docblock'><p>Returns a mutable reference to one random element of the slice, or
<code>None</code> if the slice is empty.</p>
<p>Depending on the implementation, complexity is expected to be <code>O(1)</code>.</p>
</div><h3 id='tymethod.choose_multiple' class='method'><div class="important-traits"><div class='tooltip'><span class='tooltiptext'>Important traits for <a class="struct" href="../../rand/seq/struct.SliceChooseIter.html" title="struct rand::seq::SliceChooseIter">SliceChooseIter</a>&lt;'a, S, T&gt;</span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../rand/seq/struct.SliceChooseIter.html" title="struct rand::seq::SliceChooseIter">SliceChooseIter</a>&lt;'a, S, T&gt;</h3><code class="content"><span class="where fmt-newline">impl&lt;'a, S:&nbsp;<a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/index/trait.Index.html" title="trait core::ops::index::Index">Index</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>, Output = T&gt; + ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + 'a, T:&nbsp;'a&gt; <a class="trait" href="https://doc.rust-lang.org/nightly/core/iter/traits/iterator/trait.Iterator.html" title="trait core::iter::traits::iterator::Iterator">Iterator</a> for <a class="struct" href="../../rand/seq/struct.SliceChooseIter.html" title="struct rand::seq::SliceChooseIter">SliceChooseIter</a>&lt;'a, S, T&gt;</span><span class="where fmt-newline"> type <a href='https://doc.rust-lang.org/nightly/core/iter/traits/iterator/trait.Iterator.html#associatedtype.Item' class="type">Item</a> = <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;'a </a>T;</span></code></div></div><code id='choose_multiple.v'>fn <a href='#tymethod.choose_multiple' class='fnname'>choose_multiple</a>&lt;R:&nbsp;?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>&gt;(<br>&nbsp;&nbsp;&nbsp;&nbsp;&amp;self, <br>&nbsp;&nbsp;&nbsp;&nbsp;rng: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;mut </a>R, <br>&nbsp;&nbsp;&nbsp;&nbsp;amount: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a><br>) -&gt; <a class="struct" href="../../rand/seq/struct.SliceChooseIter.html" title="struct rand::seq::SliceChooseIter">SliceChooseIter</a>&lt;Self, Self::<a class="type" href="../../rand/seq/trait.SliceRandom.html#associatedtype.Item" title="type rand::seq::SliceRandom::Item">Item</a>&gt; <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;R: <a class="trait" href="../../rand/trait.Rng.html" title="trait rand::Rng">Rng</a>,&nbsp;</span></code></h3><div class='docblock'><p>Produces an iterator that chooses <code>amount</code> elements from the slice at
random without repeating any, and returns them in random order.</p>
<p>In case this API is not sufficiently flexible, use <code>index::sample</code> then
apply the indices to the slice.</p>
<p>Complexity is expected to be the same as <code>index::sample</code>.</p>
<h1 id="example-1" class="section-header"><a href="#example-1">Example</a></h1>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">rand</span>::<span class="ident">seq</span>::<span class="ident">SliceRandom</span>;
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">rng</span> <span class="op">=</span> <span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">rand</span>::<span class="ident">thread_rng</span>();
<span class="kw">let</span> <span class="ident">sample</span> <span class="op">=</span> <span class="string">&quot;Hello, audience!&quot;</span>.<span class="ident">as_bytes</span>();
<span class="comment">// collect the results into a vector:</span>
<span class="kw">let</span> <span class="ident">v</span>: <span class="ident">Vec</span><span class="op">&lt;</span><span class="ident">u8</span><span class="op">&gt;</span> <span class="op">=</span> <span class="ident">sample</span>.<span class="ident">choose_multiple</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">rng</span>, <span class="number">3</span>).<span class="ident">cloned</span>().<span class="ident">collect</span>();
<span class="comment">// store in a buffer:</span>
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">buf</span> <span class="op">=</span> [<span class="number">0u8</span>; <span class="number">5</span>];
<span class="kw">for</span> (<span class="ident">b</span>, <span class="ident">slot</span>) <span class="kw">in</span> <span class="ident">sample</span>.<span class="ident">choose_multiple</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">rng</span>, <span class="ident">buf</span>.<span class="ident">len</span>()).<span class="ident">zip</span>(<span class="ident">buf</span>.<span class="ident">iter_mut</span>()) {
<span class="kw-2">*</span><span class="ident">slot</span> <span class="op">=</span> <span class="kw-2">*</span><span class="ident">b</span>;
}</pre></div>
</div><h3 id='tymethod.choose_weighted' class='method'><code id='choose_weighted.v'>fn <a href='#tymethod.choose_weighted' class='fnname'>choose_weighted</a>&lt;R:&nbsp;?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>, F, B, X&gt;(<br>&nbsp;&nbsp;&nbsp;&nbsp;&amp;self, <br>&nbsp;&nbsp;&nbsp;&nbsp;rng: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;mut </a>R, <br>&nbsp;&nbsp;&nbsp;&nbsp;weight: F<br>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;&amp;Self::<a class="type" href="../../rand/seq/trait.SliceRandom.html#associatedtype.Item" title="type rand::seq::SliceRandom::Item">Item</a>, <a class="enum" href="../../rand/distributions/enum.WeightedError.html" title="enum rand::distributions::WeightedError">WeightedError</a>&gt; <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;R: <a class="trait" href="../../rand/trait.Rng.html" title="trait rand::Rng">Rng</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.Fn.html" title="trait core::ops::function::Fn">Fn</a>(&amp;Self::<a class="type" href="../../rand/seq/trait.SliceRandom.html#associatedtype.Item" title="type rand::seq::SliceRandom::Item">Item</a>) -&gt; B,<br>&nbsp;&nbsp;&nbsp;&nbsp;B: <a class="trait" href="../../rand/distributions/uniform/trait.SampleBorrow.html" title="trait rand::distributions::uniform::SampleBorrow">SampleBorrow</a>&lt;X&gt;,<br>&nbsp;&nbsp;&nbsp;&nbsp;X: <a class="trait" href="../../rand/distributions/uniform/trait.SampleUniform.html" title="trait rand::distributions::uniform::SampleUniform">SampleUniform</a> + for&lt;'a&gt; <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/arith/trait.AddAssign.html" title="trait core::ops::arith::AddAssign">AddAssign</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;'a </a>X&gt; + <a class="trait" href="https://doc.rust-lang.org/nightly/core/cmp/trait.PartialOrd.html" title="trait core::cmp::PartialOrd">PartialOrd</a>&lt;X&gt; + <a class="trait" href="https://doc.rust-lang.org/nightly/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/default/trait.Default.html" title="trait core::default::Default">Default</a>,&nbsp;</span></code></h3><div class='docblock'><p>Similar to <a href="SliceRandom::choose"><code>choose</code></a>, where the likelihood of each outcome may be
specified. The specified function <code>weight</code> maps items <code>x</code> to a relative
likelihood <code>weight(x)</code>. The probability of each item being selected is
therefore <code>weight(x) / s</code>, where <code>s</code> is the sum of all <code>weight(x)</code>.</p>
<h1 id="example-2" class="section-header"><a href="#example-2">Example</a></h1>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">rand</span>::<span class="ident">prelude</span>::<span class="kw-2">*</span>;
<span class="kw">let</span> <span class="ident">choices</span> <span class="op">=</span> [(<span class="string">&#39;a&#39;</span>, <span class="number">2</span>), (<span class="string">&#39;b&#39;</span>, <span class="number">1</span>), (<span class="string">&#39;c&#39;</span>, <span class="number">1</span>)];
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">rng</span> <span class="op">=</span> <span class="ident">thread_rng</span>();
<span class="comment">// 50% chance to print &#39;a&#39;, 25% chance to print &#39;b&#39;, 25% chance to print &#39;c&#39;</span>
<span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;{:?}&quot;</span>, <span class="ident">choices</span>.<span class="ident">choose_weighted</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">rng</span>, <span class="op">|</span><span class="ident">item</span><span class="op">|</span> <span class="ident">item</span>.<span class="number">1</span>).<span class="ident">unwrap</span>().<span class="number">0</span>);</pre></div>
</div><h3 id='tymethod.choose_weighted_mut' class='method'><code id='choose_weighted_mut.v'>fn <a href='#tymethod.choose_weighted_mut' class='fnname'>choose_weighted_mut</a>&lt;R:&nbsp;?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>, F, B, X&gt;(<br>&nbsp;&nbsp;&nbsp;&nbsp;&amp;mut self, <br>&nbsp;&nbsp;&nbsp;&nbsp;rng: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;mut </a>R, <br>&nbsp;&nbsp;&nbsp;&nbsp;weight: F<br>) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;&amp;mut Self::<a class="type" href="../../rand/seq/trait.SliceRandom.html#associatedtype.Item" title="type rand::seq::SliceRandom::Item">Item</a>, <a class="enum" href="../../rand/distributions/enum.WeightedError.html" title="enum rand::distributions::WeightedError">WeightedError</a>&gt; <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;R: <a class="trait" href="../../rand/trait.Rng.html" title="trait rand::Rng">Rng</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.Fn.html" title="trait core::ops::function::Fn">Fn</a>(&amp;Self::<a class="type" href="../../rand/seq/trait.SliceRandom.html#associatedtype.Item" title="type rand::seq::SliceRandom::Item">Item</a>) -&gt; B,<br>&nbsp;&nbsp;&nbsp;&nbsp;B: <a class="trait" href="../../rand/distributions/uniform/trait.SampleBorrow.html" title="trait rand::distributions::uniform::SampleBorrow">SampleBorrow</a>&lt;X&gt;,<br>&nbsp;&nbsp;&nbsp;&nbsp;X: <a class="trait" href="../../rand/distributions/uniform/trait.SampleUniform.html" title="trait rand::distributions::uniform::SampleUniform">SampleUniform</a> + for&lt;'a&gt; <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/arith/trait.AddAssign.html" title="trait core::ops::arith::AddAssign">AddAssign</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;'a </a>X&gt; + <a class="trait" href="https://doc.rust-lang.org/nightly/core/cmp/trait.PartialOrd.html" title="trait core::cmp::PartialOrd">PartialOrd</a>&lt;X&gt; + <a class="trait" href="https://doc.rust-lang.org/nightly/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/default/trait.Default.html" title="trait core::default::Default">Default</a>,&nbsp;</span></code></h3><div class='docblock'><p>Similar to <a href="SliceRandom::choose_mut"><code>choose_mut</code></a>, where the likelihood of each outcome may be
specified. The specified function <code>weight</code> maps items <code>x</code> to a relative
likelihood <code>weight(x)</code>. The probability of each item being selected is
therefore <code>weight(x) / s</code>, where <code>s</code> is the sum of all <code>weight(x)</code>.</p>
<p>See also <a href="SliceRandom::choose_weighted"><code>choose_weighted</code></a>.</p>
</div><h3 id='tymethod.shuffle' class='method'><code id='shuffle.v'>fn <a href='#tymethod.shuffle' class='fnname'>shuffle</a>&lt;R:&nbsp;?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>&gt;(&amp;mut self, rng: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;mut </a>R) <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;R: <a class="trait" href="../../rand/trait.Rng.html" title="trait rand::Rng">Rng</a>,&nbsp;</span></code></h3><div class='docblock'><p>Shuffle a mutable slice in place.</p>
<p>Depending on the implementation, complexity is expected to be <code>O(1)</code>.</p>
<h1 id="example-3" class="section-header"><a href="#example-3">Example</a></h1>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">rand</span>::<span class="ident">thread_rng</span>;
<span class="kw">use</span> <span class="ident">rand</span>::<span class="ident">seq</span>::<span class="ident">SliceRandom</span>;
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">rng</span> <span class="op">=</span> <span class="ident">thread_rng</span>();
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">y</span> <span class="op">=</span> [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];
<span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;Unshuffled: {:?}&quot;</span>, <span class="ident">y</span>);
<span class="ident">y</span>.<span class="ident">shuffle</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">rng</span>);
<span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;Shuffled: {:?}&quot;</span>, <span class="ident">y</span>);</pre></div>
</div><h3 id='tymethod.partial_shuffle' class='method'><code id='partial_shuffle.v'>fn <a href='#tymethod.partial_shuffle' class='fnname'>partial_shuffle</a>&lt;R:&nbsp;?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>&gt;(<br>&nbsp;&nbsp;&nbsp;&nbsp;&amp;mut self, <br>&nbsp;&nbsp;&nbsp;&nbsp;rng: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;mut </a>R, <br>&nbsp;&nbsp;&nbsp;&nbsp;amount: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a><br>) -&gt; <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.tuple.html">(</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">&amp;mut [</a>Self::<a class="type" href="../../rand/seq/trait.SliceRandom.html#associatedtype.Item" title="type rand::seq::SliceRandom::Item">Item</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">]</a>, <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">&amp;mut [</a>Self::<a class="type" href="../../rand/seq/trait.SliceRandom.html#associatedtype.Item" title="type rand::seq::SliceRandom::Item">Item</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">]</a><a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.tuple.html">)</a> <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;R: <a class="trait" href="../../rand/trait.Rng.html" title="trait rand::Rng">Rng</a>,&nbsp;</span></code></h3><div class='docblock'><p>Shuffle a slice in place, but exit early.</p>
<p>Returns two mutable slices from the source slice. The first contains
<code>amount</code> elements randomly permuted. The second has the remaining
elements that are not fully shuffled.</p>
<p>This is an efficient method to select <code>amount</code> elements at random from
the slice, provided the slice may be mutated.</p>
<p>If you only need to choose elements randomly and <code>amount &gt; self.len()/2</code>
then you may improve performance by taking
<code>amount = values.len() - amount</code> and using only the second slice.</p>
<p>If <code>amount</code> is greater than the number of elements in the slice, this
will perform a full shuffle.</p>
<p>Complexity is expected to be <code>O(m)</code> where <code>m = amount</code>.</p>
</div></div><span class='loading-content'>Loading content...</span>
<h2 id='foreign-impls' class='small-section-header'>Implementations on Foreign Types<a href='#foreign-impls' class='anchor'></a></h2><h3 id='impl-SliceRandom-for-%5BT%5D' class='impl'><code class='in-band'>impl&lt;T&gt; <a class="trait" href="../../rand/seq/trait.SliceRandom.html" title="trait rand::seq::SliceRandom">SliceRandom</a> for <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">[</a>T<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">]</a></code><a href='#impl-SliceRandom-for-%5BT%5D' class='anchor'></a><a class='srclink' href='../../src/rand/seq/mod.rs.html#311-404' title='goto source code'>[src]</a></h3><div class='impl-items'><h4 id='associatedtype.Item-1' class="type"><code id='Item.t-1'>type <a href='#associatedtype.Item' class="type">Item</a> = T</code></h4><h4 id='method.choose' class="method hidden"><code id='choose.v-1'>fn <a href='#method.choose' class='fnname'>choose</a>&lt;R:&nbsp;?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>&gt;(&amp;self, rng: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;mut </a>R) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;&amp;Self::<a class="type" href="../../rand/seq/trait.SliceRandom.html#associatedtype.Item" title="type rand::seq::SliceRandom::Item">Item</a>&gt; <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;R: <a class="trait" href="../../rand/trait.Rng.html" title="trait rand::Rng">Rng</a>,&nbsp;</span></code><a class='srclink' href='../../src/rand/seq/mod.rs.html#314-322' title='goto source code'>[src]</a></h4><h4 id='method.choose_mut' class="method hidden"><code id='choose_mut.v-1'>fn <a href='#method.choose_mut' class='fnname'>choose_mut</a>&lt;R:&nbsp;?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>&gt;(&amp;mut self, rng: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;mut </a>R) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;&amp;mut Self::<a class="type" href="../../rand/seq/trait.SliceRandom.html#associatedtype.Item" title="type rand::seq::SliceRandom::Item">Item</a>&gt; <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;R: <a class="trait" href="../../rand/trait.Rng.html" title="trait rand::Rng">Rng</a>,&nbsp;</span></code><a class='srclink' href='../../src/rand/seq/mod.rs.html#324-333' title='goto source code'>[src]</a></h4><h4 id='method.choose_multiple' class="method hidden"><div class="important-traits"><div class='tooltip'><span class='tooltiptext'>Important traits for <a class="struct" href="../../rand/seq/struct.SliceChooseIter.html" title="struct rand::seq::SliceChooseIter">SliceChooseIter</a>&lt;'a, S, T&gt;</span></div><div class="content hidden"><h3 class="important">Important traits for <a class="struct" href="../../rand/seq/struct.SliceChooseIter.html" title="struct rand::seq::SliceChooseIter">SliceChooseIter</a>&lt;'a, S, T&gt;</h3><code class="content"><span class="where fmt-newline">impl&lt;'a, S:&nbsp;<a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/index/trait.Index.html" title="trait core::ops::index::Index">Index</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>, Output = T&gt; + ?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + 'a, T:&nbsp;'a&gt; <a class="trait" href="https://doc.rust-lang.org/nightly/core/iter/traits/iterator/trait.Iterator.html" title="trait core::iter::traits::iterator::Iterator">Iterator</a> for <a class="struct" href="../../rand/seq/struct.SliceChooseIter.html" title="struct rand::seq::SliceChooseIter"
<h2 id='implementors' class='small-section-header'>Implementors<a href='#implementors' class='anchor'></a></h2><div class='item-list' id='implementors-list'></div><span class='loading-content'>Loading content...</span><script type="text/javascript">window.inlined_types=new Set([]);</script><script type="text/javascript" async
src="../../implementors/rand/seq/trait.SliceRandom.js">
</script></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 = "rand";</script><script src="../../aliases.js"></script><script src="../../main.js"></script><script defer src="../../search-index.js"></script></body></html>