Skip to content

Commit d062fc5

Browse files
committed
Update documentation on generating arrays
1 parent 5c7bbea commit d062fc5

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

src/distributions/mod.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -289,13 +289,15 @@ impl<D, R, T> iter::TrustedLen for DistIter<D, R, T>
289289
/// * Wrapping integers (`Wrapping<T>`), besides the type identical to their
290290
/// normal integer variants.
291291
///
292-
/// The following aggregate types also implement the distribution `Standard` as
293-
/// long as their component types implement it:
292+
/// The `Standard` distribution also supports generation of the following
293+
/// compound types where all component types are supported:
294294
///
295-
/// * Tuples and arrays: Each element of the tuple or array is generated
296-
/// independently, using the `Standard` distribution recursively.
297-
/// * `Option<T>` where `Standard` is implemented for `T`: Returns `None` with
298-
/// probability 0.5; otherwise generates a random `x: T` and returns `Some(x)`.
295+
/// * Tuples (up to 12 elements): each element is generated sequentially.
296+
/// * Arrays (up to 32 elements): each element is generated sequentially;
297+
/// see also [`Rng::fill`] which supports arbitrary array length for integer
298+
/// types and tends to be faster for `u32` and smaller types.
299+
/// * `Option<T>` first generates a `bool`, and if true generates and returns
300+
/// `Some(value)` where `value: T`, otherwise returning `None`.
299301
///
300302
/// ## Custom implementations
301303
///

src/lib.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,6 @@ use distributions::uniform::{SampleUniform, UniformSampler, SampleBorrow};
135135
pub trait Rng: RngCore {
136136
/// Return a random value supporting the [`Standard`] distribution.
137137
///
138-
/// [`Standard`]: distributions::Standard
139-
///
140138
/// # Example
141139
///
142140
/// ```
@@ -147,6 +145,28 @@ pub trait Rng: RngCore {
147145
/// println!("{}", x);
148146
/// println!("{:?}", rng.gen::<(f64, bool)>());
149147
/// ```
148+
///
149+
/// # Arrays and tuples
150+
///
151+
/// The `rng.gen()` method is able to generate arrays (up to 32 elements)
152+
/// and tuples (up to 12 elements), so long as all element types can be
153+
/// generated.
154+
///
155+
/// For arrays of integers, especially for those with small element types
156+
/// (< 64 bit), it will likely be faster to instead use [`Rng::fill`].
157+
///
158+
/// ```
159+
/// use rand::{thread_rng, Rng};
160+
///
161+
/// let mut rng = thread_rng();
162+
/// let tuple: (u8, i32, char) = rng.gen(); // arbitrary tuple support
163+
///
164+
/// let arr1: [f32; 32] = rng.gen(); // array construction
165+
/// let mut arr2 = [0u8; 128];
166+
/// rng.fill(&mut arr2); // array fill
167+
/// ```
168+
///
169+
/// [`Standard`]: distributions::Standard
150170
#[inline]
151171
fn gen<T>(&mut self) -> T
152172
where Standard: Distribution<T> {

0 commit comments

Comments
 (0)