pub struct Choose<'a, T> { /* private fields */ }Expand description
A distribution to uniformly sample elements of a slice
Like IndexedRandom::choose, this uniformly samples elements of a slice
without modification of the slice (so called “sampling with replacement”).
This distribution object may be a little faster for repeated sampling (but
slower for small numbers of samples).
§Examples
Since this is a distribution, Rng::sample_iter and
Distribution::sample_iter may be used, for example:
use rand::distr::{Distribution, slice::Choose};
let vowels = ['a', 'e', 'i', 'o', 'u'];
let vowels_dist = Choose::new(&vowels).unwrap();
// build a string of 10 vowels
let vowel_string: String = vowels_dist
.sample_iter(&mut rand::rng())
.take(10)
.collect();
println!("{}", vowel_string);
assert_eq!(vowel_string.len(), 10);
assert!(vowel_string.chars().all(|c| vowels.contains(&c)));For a single sample, IndexedRandom::choose may be preferred:
use rand::seq::IndexedRandom;
let vowels = ['a', 'e', 'i', 'o', 'u'];
let mut rng = rand::rng();
println!("{}", vowels.choose(&mut rng).unwrap());Implementations§
Trait Implementations§
impl<'a, T> Copy for Choose<'a, T>where
T: Copy,
Source§impl<'a, T> Distribution<&'a T> for Choose<'a, T>
impl<'a, T> Distribution<&'a T> for Choose<'a, T>
Source§impl SampleString for Choose<'_, char>
Available on crate feature alloc only.
impl SampleString for Choose<'_, char>
Available on crate feature
alloc only.Auto Trait Implementations§
impl<'a, T> Freeze for Choose<'a, T>
impl<'a, T> RefUnwindSafe for Choose<'a, T>where
T: RefUnwindSafe,
impl<'a, T> Send for Choose<'a, T>where
T: Sync,
impl<'a, T> Sync for Choose<'a, T>where
T: Sync,
impl<'a, T> Unpin for Choose<'a, T>
impl<'a, T> UnsafeUnpin for Choose<'a, T>
impl<'a, T> UnwindSafe for Choose<'a, T>where
T: RefUnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Converts
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Converts
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Converts
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Converts
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more