Trait SliceRandom
trait SliceRandom: IndexedMutRandom
Extension trait on slices, providing shuffling methods.
This trait is implemented on all [T] slice types, providing several
methods for choosing and shuffling elements. You must use this trait:
use SliceRandom;
let mut rng = rng;
let mut bytes = "Hello, random!".to_string.into_bytes;
bytes.shuffle;
let str = Stringfrom_utf8.unwrap;
println!;
Example output (non-deterministic):
l,nmroHado !le
Required Methods
fn shuffle<R>(self: &mut Self, rng: &mut R) where R: Rng + ?SizedShuffle a mutable slice in place.
For slices of length
n, complexity isO(n). The resulting permutation is picked uniformly from the set of all possible permutations.Example
use SliceRandom; let mut rng = rng; let mut y = ; println!; y.shuffle; println!;fn partial_shuffle<R>(self: &mut Self, rng: &mut R, amount: usize) -> (&mut [<Self as >::Output], &mut [<Self as >::Output]) where <Self as >::Output: Sized, R: Rng + ?SizedShuffle a slice in place, but exit early.
Returns two mutable slices from the source slice. The first contains
amountelements randomly permuted. The second has the remaining elements that are not fully shuffled.This is an efficient method to select
amountelements at random from the slice, provided the slice may be mutated.If you only need to choose elements randomly and
amount > self.len()/2then you may improve performance by takingamount = self.len() - amountand using only the second slice.If
amountis greater than the number of elements in the slice, this will perform a full shuffle.For slices, complexity is
O(m)wherem = amount.
Implementors
impl<T> SliceRandom for [T]