Skip to main content

SampleRange

Trait SampleRange 

Source
pub trait SampleRange<T> {
    // Required methods
    fn sample_single<R: Rng + ?Sized>(self, rng: &mut R) -> Result<T, Error>;
    fn is_empty(&self) -> bool;
}
Expand description

Range that supports generating a single sample efficiently.

See RngExt::random_range for examples; the method provides a convenient interface over this type.

§Examples

use rand::distr::uniform::SampleRange;
let mut rng = rand::rng();

// Range and InclusiveRange are supported for many types:
assert_eq!((9..10).sample_single(&mut rng), Ok(9));
assert_eq!(('a'..='a').sample_single(&mut rng), Ok('a'));

// RangeTo and RangeToInclusive are supported for unsigned integers:
assert_eq!((..1u8).sample_single(&mut rng), Ok(0));
assert!((..=10u32).sample_single(&mut rng).unwrap() <= 10);

Required Methods§

Source

fn sample_single<R: Rng + ?Sized>(self, rng: &mut R) -> Result<T, Error>

Generate a sample from the given range.

Source

fn is_empty(&self) -> bool

Check whether the range is empty.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl SampleRange<u8> for RangeTo<u8>

Source§

fn sample_single<R: Rng + ?Sized>(self, rng: &mut R) -> Result<u8, Error>

Source§

fn is_empty(&self) -> bool

Source§

impl SampleRange<u8> for RangeToInclusive<u8>

Source§

fn sample_single<R: Rng + ?Sized>(self, rng: &mut R) -> Result<u8, Error>

Source§

fn is_empty(&self) -> bool

Source§

impl SampleRange<u16> for RangeTo<u16>

Source§

fn sample_single<R: Rng + ?Sized>(self, rng: &mut R) -> Result<u16, Error>

Source§

fn is_empty(&self) -> bool

Source§

impl SampleRange<u16> for RangeToInclusive<u16>

Source§

fn sample_single<R: Rng + ?Sized>(self, rng: &mut R) -> Result<u16, Error>

Source§

fn is_empty(&self) -> bool

Source§

impl SampleRange<u32> for RangeTo<u32>

Source§

fn sample_single<R: Rng + ?Sized>(self, rng: &mut R) -> Result<u32, Error>

Source§

fn is_empty(&self) -> bool

Source§

impl SampleRange<u32> for RangeToInclusive<u32>

Source§

fn sample_single<R: Rng + ?Sized>(self, rng: &mut R) -> Result<u32, Error>

Source§

fn is_empty(&self) -> bool

Source§

impl SampleRange<u64> for RangeTo<u64>

Source§

fn sample_single<R: Rng + ?Sized>(self, rng: &mut R) -> Result<u64, Error>

Source§

fn is_empty(&self) -> bool

Source§

impl SampleRange<u64> for RangeToInclusive<u64>

Source§

fn sample_single<R: Rng + ?Sized>(self, rng: &mut R) -> Result<u64, Error>

Source§

fn is_empty(&self) -> bool

Source§

impl SampleRange<u128> for RangeTo<u128>

Source§

fn sample_single<R: Rng + ?Sized>(self, rng: &mut R) -> Result<u128, Error>

Source§

fn is_empty(&self) -> bool

Source§

impl SampleRange<u128> for RangeToInclusive<u128>

Source§

fn sample_single<R: Rng + ?Sized>(self, rng: &mut R) -> Result<u128, Error>

Source§

fn is_empty(&self) -> bool

Source§

impl SampleRange<usize> for RangeTo<usize>

Source§

impl SampleRange<usize> for RangeToInclusive<usize>

Source§

impl<T: SampleUniform + PartialOrd> SampleRange<T> for Range<T>

Source§

fn sample_single<R: Rng + ?Sized>(self, rng: &mut R) -> Result<T, Error>

Source§

fn is_empty(&self) -> bool

Source§

impl<T: SampleUniform + PartialOrd> SampleRange<T> for RangeInclusive<T>

Source§

fn sample_single<R: Rng + ?Sized>(self, rng: &mut R) -> Result<T, Error>

Source§

fn is_empty(&self) -> bool

Implementors§