Files
GopherGate/target/doc/rand/seq/trait.IteratorRandom.html
2026-02-26 12:00:21 -05:00

86 lines
19 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 iterators, providing random sampling methods."><title>IteratorRandom 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="#">IteratorRandom</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="#">Iterator<wbr>Random</a></h2><h3><a href="#provided-methods">Provided Methods</a></h3><ul class="block"><li><a href="#method.choose" title="choose">choose</a></li><li><a href="#method.choose_multiple" title="choose_multiple">choose_multiple</a></li><li><a href="#method.choose_multiple_fill" title="choose_multiple_fill">choose_multiple_fill</a></li><li><a href="#method.choose_stable" title="choose_stable">choose_stable</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">Iterator<wbr>Random</span>&nbsp;<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/iterator.rs.html#34-269">Source</a> </span></div><pre class="rust item-decl"><code>pub trait IteratorRandom: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/iter/traits/iterator/trait.Iterator.html" title="trait core::iter::traits::iterator::Iterator">Iterator</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> {
// Provided methods
fn <a href="#method.choose" class="fn">choose</a>&lt;R&gt;(self, rng: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.reference.html">&amp;mut R</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/1.93.1/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.93.1/core/iter/traits/iterator/trait.Iterator.html#associatedtype.Item" title="type core::iter::traits::iterator::Iterator::Item">Item</a>&gt;
<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="#method.choose_stable" class="fn">choose_stable</a>&lt;R&gt;(self, rng: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.reference.html">&amp;mut R</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/1.93.1/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.93.1/core/iter/traits/iterator/trait.Iterator.html#associatedtype.Item" title="type core::iter::traits::iterator::Iterator::Item">Item</a>&gt;
<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="#method.choose_multiple_fill" class="fn">choose_multiple_fill</a>&lt;R&gt;(
self,
rng: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.reference.html">&amp;mut R</a>,
buf: &amp;mut [Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.93.1/core/iter/traits/iterator/trait.Iterator.html#associatedtype.Item" title="type core::iter::traits::iterator::Iterator::Item">Item</a>],
) -&gt; <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.usize.html">usize</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="#method.choose_multiple" class="fn">choose_multiple</a>&lt;R&gt;(self, rng: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.reference.html">&amp;mut R</a>, amount: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.usize.html">usize</a>) -&gt; <a class="struct" href="https://doc.rust-lang.org/1.93.1/alloc/vec/struct.Vec.html" title="struct alloc::vec::Vec">Vec</a>&lt;Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.93.1/core/iter/traits/iterator/trait.Iterator.html#associatedtype.Item" title="type core::iter::traits::iterator::Iterator::Item">Item</a>&gt;
<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> { ... }
}</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Extension trait on iterators, providing random sampling methods.</p>
<p>This trait is implemented on all iterators <code>I</code> where <code>I: Iterator + Sized</code>
and provides methods for
choosing one or more 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::IteratorRandom;
<span class="kw">let </span>faces = <span class="string">"😀😎😐😕😠😢"</span>;
<span class="macro">println!</span>(<span class="string">"I am {}!"</span>, faces.chars().choose(<span class="kw-2">&amp;mut </span>rand::rng()).unwrap());</code></pre></div>
<p>Example output (non-deterministic):</p>
<div class="example-wrap"><pre class="language-none"><code>I am 😀!</code></pre></div></div></details><h2 id="provided-methods" class="section-header">Provided Methods<a href="#provided-methods" class="anchor">§</a></h2><div class="methods"><details class="toggle method-toggle" open><summary><section id="method.choose" class="method"><a class="src rightside" href="../../src/rand/seq/iterator.rs.html#66-119">Source</a><h4 class="code-header">fn <a href="#method.choose" class="fn">choose</a>&lt;R&gt;(self, rng: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.reference.html">&amp;mut R</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/1.93.1/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.93.1/core/iter/traits/iterator/trait.Iterator.html#associatedtype.Item" title="type core::iter::traits::iterator::Iterator::Item">Item</a>&gt;<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>Uniformly sample one element</p>
<p>Assuming that the <a href="https://doc.rust-lang.org/1.93.1/core/iter/traits/iterator/trait.Iterator.html#method.size_hint" title="method core::iter::traits::iterator::Iterator::size_hint"><code>Iterator::size_hint</code></a> is correct, this method
returns one uniformly-sampled random element of the slice, or <code>None</code>
only if the slice is empty. Incorrect bounds on the <code>size_hint</code> may
cause this method to incorrectly return <code>None</code> if fewer elements than
the advertised <code>lower</code> bound are present and may prevent sampling of
elements beyond an advertised <code>upper</code> bound (i.e. incorrect <code>size_hint</code>
is memory-safe, but may result in unexpected <code>None</code> result and
non-uniform distribution).</p>
<p>With an accurate <a href="https://doc.rust-lang.org/1.93.1/core/iter/traits/iterator/trait.Iterator.html#method.size_hint" title="method core::iter::traits::iterator::Iterator::size_hint"><code>Iterator::size_hint</code></a> and where <a href="https://doc.rust-lang.org/1.93.1/core/iter/traits/iterator/trait.Iterator.html#method.nth" title="method core::iter::traits::iterator::Iterator::nth"><code>Iterator::nth</code></a> is
a constant-time operation, this method can offer <code>O(1)</code> performance.
Where no size hint is
available, complexity is <code>O(n)</code> where <code>n</code> is the iterator length.
Partial hints (where <code>lower &gt; 0</code>) also improve performance.</p>
<p>Note further that <a href="https://doc.rust-lang.org/1.93.1/core/iter/traits/iterator/trait.Iterator.html#method.size_hint" title="method core::iter::traits::iterator::Iterator::size_hint"><code>Iterator::size_hint</code></a> may affect the number of RNG
samples used as well as the result (while remaining uniform sampling).
Consider instead using <a href="trait.IteratorRandom.html#method.choose_stable" title="method rand::seq::IteratorRandom::choose_stable"><code>IteratorRandom::choose_stable</code></a> to avoid
<a href="https://doc.rust-lang.org/1.93.1/core/iter/traits/iterator/trait.Iterator.html" title="trait core::iter::traits::iterator::Iterator"><code>Iterator</code></a> combinators which only change size hints from affecting the
results.</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::IteratorRandom;
<span class="kw">let </span>words = <span class="string">"Mary had a little lamb"</span>.split(<span class="string">' '</span>);
<span class="macro">println!</span>(<span class="string">"{}"</span>, words.choose(<span class="kw-2">&amp;mut </span>rand::rng()).unwrap());</code></pre></div></div></details><details class="toggle method-toggle" open><summary><section id="method.choose_stable" class="method"><a class="src rightside" href="../../src/rand/seq/iterator.rs.html#145-186">Source</a><h4 class="code-header">fn <a href="#method.choose_stable" class="fn">choose_stable</a>&lt;R&gt;(self, rng: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.reference.html">&amp;mut R</a>) -&gt; <a class="enum" href="https://doc.rust-lang.org/1.93.1/core/option/enum.Option.html" title="enum core::option::Option">Option</a>&lt;Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.93.1/core/iter/traits/iterator/trait.Iterator.html#associatedtype.Item" title="type core::iter::traits::iterator::Iterator::Item">Item</a>&gt;<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>Uniformly sample one element (stable)</p>
<p>This method is very similar to <a href="trait.IteratorRandom.html#method.choose" title="method rand::seq::IteratorRandom::choose"><code>choose</code></a> except that the result
only depends on the length of the iterator and the values produced by
<code>rng</code>. Notably for any iterator of a given length this will make the
same requests to <code>rng</code> and if the same sequence of values are produced
the same index will be selected from <code>self</code>. This may be useful if you
need consistent results no matter what type of iterator you are working
with. If you do not need this stability prefer <a href="trait.IteratorRandom.html#method.choose" title="method rand::seq::IteratorRandom::choose"><code>choose</code></a>.</p>
<p>Note that this method still uses <a href="https://doc.rust-lang.org/1.93.1/core/iter/traits/iterator/trait.Iterator.html#method.size_hint" title="method core::iter::traits::iterator::Iterator::size_hint"><code>Iterator::size_hint</code></a> to skip
constructing elements where possible, however the selection and <code>rng</code>
calls are the same in the face of this optimization. If you want to
force every element to be created regardless call <code>.inspect(|e| ())</code>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.choose_multiple_fill" class="method"><a class="src rightside" href="../../src/rand/seq/iterator.rs.html#203-227">Source</a><h4 class="code-header">fn <a href="#method.choose_multiple_fill" class="fn">choose_multiple_fill</a>&lt;R&gt;(self, rng: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.reference.html">&amp;mut R</a>, buf: &amp;mut [Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.93.1/core/iter/traits/iterator/trait.Iterator.html#associatedtype.Item" title="type core::iter::traits::iterator::Iterator::Item">Item</a>]) -&gt; <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.usize.html">usize</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>Uniformly sample <code>amount</code> distinct elements into a buffer</p>
<p>Collects values at random from the iterator into a supplied buffer
until that buffer is filled.</p>
<p>Although the elements are selected randomly, the order of elements in
the buffer is neither stable nor fully random. If random ordering is
desired, shuffle the result.</p>
<p>Returns the number of elements added to the buffer. This equals the length
of the buffer unless the iterator contains insufficient elements, in which
case this equals the number of elements available.</p>
<p>Complexity is <code>O(n)</code> where <code>n</code> is the length of the iterator.
For slices, prefer <a href="trait.IndexedRandom.html#method.choose_multiple" title="method rand::seq::IndexedRandom::choose_multiple"><code>IndexedRandom::choose_multiple</code></a>.</p>
</div></details><details class="toggle method-toggle" open><summary><section id="method.choose_multiple" class="method"><a class="src rightside" href="../../src/rand/seq/iterator.rs.html#244-268">Source</a><h4 class="code-header">fn <a href="#method.choose_multiple" class="fn">choose_multiple</a>&lt;R&gt;(self, rng: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.reference.html">&amp;mut R</a>, amount: <a class="primitive" href="https://doc.rust-lang.org/1.93.1/std/primitive.usize.html">usize</a>) -&gt; <a class="struct" href="https://doc.rust-lang.org/1.93.1/alloc/vec/struct.Vec.html" title="struct alloc::vec::Vec">Vec</a>&lt;Self::<a class="associatedtype" href="https://doc.rust-lang.org/1.93.1/core/iter/traits/iterator/trait.Iterator.html#associatedtype.Item" title="type core::iter::traits::iterator::Iterator::Item">Item</a>&gt;<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>Uniformly sample <code>amount</code> distinct elements into a <a href="https://doc.rust-lang.org/1.93.1/alloc/vec/struct.Vec.html" title="struct alloc::vec::Vec"><code>Vec</code></a></p>
<p>This is equivalent to <code>choose_multiple_fill</code> except for the result type.</p>
<p>Although the elements are selected randomly, the order of elements in
the buffer is neither stable nor fully random. If random ordering is
desired, shuffle the result.</p>
<p>The length of the returned vector equals <code>amount</code> unless the iterator
contains insufficient elements, in which case it equals the number of
elements available.</p>
<p>Complexity is <code>O(n)</code> where <code>n</code> is the length of the iterator.
For slices, prefer <a href="trait.IndexedRandom.html#method.choose_multiple" title="method rand::seq::IndexedRandom::choose_multiple"><code>IndexedRandom::choose_multiple</code></a>.</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="implementors" class="section-header">Implementors<a href="#implementors" class="anchor">§</a></h2><div id="implementors-list"><section id="impl-IteratorRandom-for-I" class="impl"><a class="src rightside" href="../../src/rand/seq/iterator.rs.html#271">Source</a><a href="#impl-IteratorRandom-for-I" class="anchor">§</a><h3 class="code-header">impl&lt;I&gt; <a class="trait" href="trait.IteratorRandom.html" title="trait rand::seq::IteratorRandom">IteratorRandom</a> for I<div class="where">where
I: <a class="trait" href="https://doc.rust-lang.org/1.93.1/core/iter/traits/iterator/trait.Iterator.html" title="trait core::iter::traits::iterator::Iterator">Iterator</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></h3></section></div><script src="../../trait.impl/rand/seq/iterator/trait.IteratorRandom.js" async></script></section></div></main></body></html>