Skip to main content

SampleRange

Trait SampleRange 

pub trait SampleRange<T> {
    // Required methods
    fn sample_single<R>(self, rng: &mut R) -> Result<T, Error>
       where R: Rng + ?Sized;
    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§

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

Generate a sample from the given range.

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", so this trait is not object safe.

Implementations on Foreign Types§

§

impl SampleRange<u8> for RangeTo<u8>

§

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

§

fn is_empty(&self) -> bool

§

impl SampleRange<u8> for RangeToInclusive<u8>

§

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

§

fn is_empty(&self) -> bool

§

impl SampleRange<u16> for RangeTo<u16>

§

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

§

fn is_empty(&self) -> bool

§

impl SampleRange<u16> for RangeToInclusive<u16>

§

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

§

fn is_empty(&self) -> bool

§

impl SampleRange<u32> for RangeTo<u32>

§

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

§

fn is_empty(&self) -> bool

§

impl SampleRange<u32> for RangeToInclusive<u32>

§

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

§

fn is_empty(&self) -> bool

§

impl SampleRange<u64> for RangeTo<u64>

§

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

§

fn is_empty(&self) -> bool

§

impl SampleRange<u64> for RangeToInclusive<u64>

§

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

§

fn is_empty(&self) -> bool

§

impl SampleRange<u128> for RangeTo<u128>

§

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

§

fn is_empty(&self) -> bool

§

impl SampleRange<u128> for RangeToInclusive<u128>

§

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

§

fn is_empty(&self) -> bool

§

impl SampleRange<usize> for RangeTo<usize>

§

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

§

fn is_empty(&self) -> bool

§

impl SampleRange<usize> for RangeToInclusive<usize>

§

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

§

fn is_empty(&self) -> bool

§

impl<T> SampleRange<T> for Range<T>

§

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

§

fn is_empty(&self) -> bool

§

impl<T> SampleRange<T> for RangeInclusive<T>

§

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

§

fn is_empty(&self) -> bool

Implementors§