59 lines
13 KiB
HTML
59 lines
13 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="Extension trait on slices, providing shuffling methods."><title>SliceRandom in rand::seq - 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="rand" 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="icon" href="https://www.rust-lang.org/favicon.ico"></head><body class="rustdoc trait"><!--[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="#">SliceRandom</a></h2></rustdoc-topbar><nav class="sidebar"><div class="sidebar-crate"><a class="logo-container" href="../../rand/index.html"><img src="https://www.rust-lang.org/logos/rust-logo-128x128-blk.png" alt="logo"></a><h2><a href="../../rand/index.html">rand</a><span class="version">0.9.2</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">Slice<wbr>Random</a></h2><h3><a href="#required-methods">Required Methods</a></h3><ul class="block"><li><a href="#tymethod.partial_shuffle" title="partial_shuffle">partial_shuffle</a></li><li><a href="#tymethod.shuffle" title="shuffle">shuffle</a></li></ul><h3><a href="#foreign-impls">Implementations on Foreign Types</a></h3><ul class="block"><li><a href="#impl-SliceRandom-for-%5BT%5D" title="[T]">[T]</a></li></ul><h3><a href="#dyn-compatibility">Dyn Compatibility</a></h3><h3><a href="#implementors">Implementors</a></h3></section><div id="rustdoc-modnav"><h2><a href="index.html">In rand::<wbr>seq</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">rand</a>::<wbr><a href="index.html">seq</a></div><h1>Trait <span class="trait">Slice<wbr>Random</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/rand/seq/slice.rs.html#316-362">Source</a> </span></div><pre class="rust item-decl"><code>pub trait SliceRandom: <a class="trait" href="trait.IndexedMutRandom.html" title="trait rand::seq::IndexedMutRandom">IndexedMutRandom</a> {
|
|
// Required methods
|
|
fn <a href="#tymethod.shuffle" class="fn">shuffle</a><R>(&mut self, rng: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.reference.html">&mut R</a>)
|
|
<span class="where">where R: <a class="trait" href="../trait.Rng.html" title="trait rand::Rng">Rng</a> + ?<a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a></span>;
|
|
<span class="item-spacer"></span> fn <a href="#tymethod.partial_shuffle" class="fn">partial_shuffle</a><R>(
|
|
&mut self,
|
|
rng: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.reference.html">&mut R</a>,
|
|
amount: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.usize.html">usize</a>,
|
|
) -> (&mut [Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.93.1/core/ops/index/trait.Index.html#associatedtype.Output" title="type core::ops::index::Index::Output">Output</a>], &mut [Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.93.1/core/ops/index/trait.Index.html#associatedtype.Output" title="type core::ops::index::Index::Output">Output</a>])
|
|
<span class="where">where Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.93.1/core/ops/index/trait.Index.html#associatedtype.Output" title="type core::ops::index::Index::Output">Output</a>: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,
|
|
R: <a class="trait" href="../trait.Rng.html" title="trait rand::Rng">Rng</a> + ?<a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a></span>;
|
|
}</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Extension trait on slices, providing shuffling methods.</p>
|
|
<p>This trait is implemented on all <code>[T]</code> slice types, providing several
|
|
methods for choosing and shuffling elements. You must <code>use</code> this trait:</p>
|
|
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>rand::seq::SliceRandom;
|
|
|
|
<span class="kw">let </span><span class="kw-2">mut </span>rng = rand::rng();
|
|
<span class="kw">let </span><span class="kw-2">mut </span>bytes = <span class="string">"Hello, random!"</span>.to_string().into_bytes();
|
|
bytes.shuffle(<span class="kw-2">&mut </span>rng);
|
|
<span class="kw">let </span>str = String::from_utf8(bytes).unwrap();
|
|
<span class="macro">println!</span>(<span class="string">"{}"</span>, str);</code></pre></div>
|
|
<p>Example output (non-deterministic):</p>
|
|
<div class="example-wrap"><pre class="language-none"><code>l,nmroHado !le</code></pre></div></div></details><h2 id="required-methods" class="section-header">Required Methods<a href="#required-methods" class="anchor">§</a></h2><div class="methods"><details class="toggle method-toggle" open><summary><section id="tymethod.shuffle" class="method"><a class="src rightside" href="../../src/rand/seq/slice.rs.html#333-335">Source</a><h4 class="code-header">fn <a href="#tymethod.shuffle" class="fn">shuffle</a><R>(&mut self, rng: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.reference.html">&mut R</a>)<div class="where">where
|
|
R: <a class="trait" href="../trait.Rng.html" title="trait rand::Rng">Rng</a> + ?<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></h4></section></summary><div class="docblock"><p>Shuffle a mutable slice in place.</p>
|
|
<p>For slices of length <code>n</code>, complexity is <code>O(n)</code>.
|
|
The resulting permutation is picked uniformly from the set of all possible permutations.</p>
|
|
<h5 id="example"><a class="doc-anchor" href="#example">§</a>Example</h5>
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>rand::seq::SliceRandom;
|
|
|
|
<span class="kw">let </span><span class="kw-2">mut </span>rng = rand::rng();
|
|
<span class="kw">let </span><span class="kw-2">mut </span>y = [<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="string">"Unshuffled: {:?}"</span>, y);
|
|
y.shuffle(<span class="kw-2">&mut </span>rng);
|
|
<span class="macro">println!</span>(<span class="string">"Shuffled: {:?}"</span>, y);</code></pre></div></div></details><details class="toggle method-toggle" open><summary><section id="tymethod.partial_shuffle" class="method"><a class="src rightside" href="../../src/rand/seq/slice.rs.html#354-361">Source</a><h4 class="code-header">fn <a href="#tymethod.partial_shuffle" class="fn">partial_shuffle</a><R>(
|
|
&mut self,
|
|
rng: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.reference.html">&mut R</a>,
|
|
amount: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.usize.html">usize</a>,
|
|
) -> (&mut [Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.93.1/core/ops/index/trait.Index.html#associatedtype.Output" title="type core::ops::index::Index::Output">Output</a>], &mut [Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.93.1/core/ops/index/trait.Index.html#associatedtype.Output" title="type core::ops::index::Index::Output">Output</a>])<div class="where">where
|
|
Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.93.1/core/ops/index/trait.Index.html#associatedtype.Output" title="type core::ops::index::Index::Output">Output</a>: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,
|
|
R: <a class="trait" href="../trait.Rng.html" title="trait rand::Rng">Rng</a> + ?<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></h4></section></summary><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 > self.len()/2</code>
|
|
then you may improve performance by taking
|
|
<code>amount = self.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>For slices, complexity is <code>O(m)</code> where <code>m = amount</code>.</p>
|
|
</div></details></div><h2 id="dyn-compatibility" class="section-header">Dyn Compatibility<a href="#dyn-compatibility" class="anchor">§</a></h2><div class="dyn-compatibility-info"><p>This trait is <b>not</b> <a href="https://doc.rust-lang.org/1.93.1/reference/items/traits.html#dyn-compatibility">dyn compatible</a>.</p><p><i>In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.</i></p></div><h2 id="foreign-impls" class="section-header">Implementations on Foreign Types<a href="#foreign-impls" class="anchor">§</a></h2><details class="toggle implementors-toggle"><summary><section id="impl-SliceRandom-for-%5BT%5D" class="impl"><a class="src rightside" href="../../src/rand/seq/slice.rs.html#372-414">Source</a><a href="#impl-SliceRandom-for-%5BT%5D" class="anchor">§</a><h3 class="code-header">impl<T> <a class="trait" href="trait.SliceRandom.html" title="trait rand::seq::SliceRandom">SliceRandom</a> for <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.slice.html">[T]</a></h3></section></summary><div class="impl-items"><section id="method.shuffle" class="method trait-impl"><a class="src rightside" href="../../src/rand/seq/slice.rs.html#373-382">Source</a><a href="#method.shuffle" class="anchor">§</a><h4 class="code-header">fn <a href="#tymethod.shuffle" class="fn">shuffle</a><R>(&mut self, rng: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.reference.html">&mut R</a>)<div class="where">where
|
|
R: <a class="trait" href="../trait.Rng.html" title="trait rand::Rng">Rng</a> + ?<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></h4></section><section id="method.partial_shuffle" class="method trait-impl"><a class="src rightside" href="../../src/rand/seq/slice.rs.html#384-413">Source</a><a href="#method.partial_shuffle" class="anchor">§</a><h4 class="code-header">fn <a href="#tymethod.partial_shuffle" class="fn">partial_shuffle</a><R>(
|
|
&mut self,
|
|
rng: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.reference.html">&mut R</a>,
|
|
amount: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.usize.html">usize</a>,
|
|
) -> (&mut <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.slice.html">[T]</a>, &mut <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.slice.html">[T]</a>)<div class="where">where
|
|
R: <a class="trait" href="../trait.Rng.html" title="trait rand::Rng">Rng</a> + ?<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></h4></section></div></details><h2 id="implementors" class="section-header">Implementors<a href="#implementors" class="anchor">§</a></h2><div id="implementors-list"></div><script src="../../trait.impl/rand/seq/slice/trait.SliceRandom.js" data-ignore-extern-crates="std" async></script></section></div></main></body></html> |