Skip to content

Commit f1130cc

Browse files
committed
Some changes to distributions module documentation
1 parent 4a5087f commit f1130cc

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

src/distributions/mod.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,21 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
//! Generating random samples from [probability distributions].
11+
//! Generating random samples from probability distributions.
1212
//!
1313
//! This module is the home of the [`Distribution`] trait and several of its
14-
//! implementations, which is the workhorse behind some of the convenient
14+
//! implementations. It is the workhorse behind some of the convenient
1515
//! functionality of the [`Rng`] trait, including [`gen`], [`gen_range`] and
1616
//! of course [`sample`].
17-
//!
18-
//! Abstractly, a probability distribution describes the probability of
17+
//!
18+
//! Abstractly, a [probability distribution] describes the probability of
1919
//! occurance of each value in its sample space.
20+
//!
2021
//! More concretely, an implementation of `Distribution<T>` for type `X` is an
2122
//! algorithm for choosing values from the sample space (a subset of `T`)
2223
//! according to the distribution `X` represents, using an external source of
2324
//! randomness (an RNG supplied to the `sample` function).
25+
//!
2426
//! A type `X` may implement `Distribution<T>` for multiple types `T`.
2527
//! Any type implementing [`Distribution`] is stateless (i.e. immutable),
2628
//! but it may have internal parameters set at construction time (for example,
@@ -35,9 +37,9 @@
3537
//! types, tuples, arrays, and a few derived types. See the documentation of
3638
//! [`Standard`] for more details.
3739
//!
38-
//! It is possible to implement `Distribution<T>` for [`Standard`] for user
39-
//! types `T`; doing so makes it possible to generate type `T` with
40-
//! [`Rng::gen()`], and by extension also with the [`random()`] function.
40+
//! Implementing `Distribution<T>` for [`Standard`] for user types `T` makes it
41+
//! possible to generate type `T` with [`Rng::gen()`], and by extension also
42+
//! with the [`random()`] function.
4143
//!
4244
//!
4345
//! # Distribution to sample from a `Uniform` range
@@ -49,9 +51,9 @@
4951
//!
5052
//! Values may be sampled from this distribution using [`Rng::gen_range`] or
5153
//! by creating a distribution object with [`Uniform::new`],
52-
//! [`Uniform::new_inclusive`] or `From<Range>`;
53-
//! when the range limits are not known at compile time it is typically faster
54-
//! to reuse an existing distribution object than to call [`Rng::gen_range`].
54+
//! [`Uniform::new_inclusive`] or `From<Range>`. When the range limits are not
55+
//! known at compile time it is typically faster to reuse an existing
56+
//! distribution object than to call [`Rng::gen_range`].
5557
//!
5658
//! User types `T` may also implement `Distribution<T>` for [`Uniform`],
5759
//! although this is less straightforward than for [`Standard`] (see the
@@ -126,8 +128,8 @@
126128
//! }
127129
//! ```
128130
//!
129-
//!
130-
//! [probability distributions]: https://en.wikipedia.org/wiki/Probability_distribution
131+
//!
132+
//! [probability distribution]: https://en.wikipedia.org/wiki/Probability_distribution
131133
//! [`Distribution`]: trait.Distribution.html
132134
//! [`gen_range`]: ../trait.Rng.html#method.gen_range
133135
//! [`gen`]: ../trait.Rng.html#method.gen

src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,10 @@ pub trait Rng: RngCore {
373373
/// use rand::distributions::Uniform;
374374
///
375375
/// let mut rng = thread_rng();
376-
/// let x: i32 = rng.sample(Uniform::new(10, 15));
376+
/// let x = rng.sample(Uniform::new(10u32, 15));
377+
/// // Type annotation requires two types, the type and distribution; the
378+
/// // distribution can be inferred.
379+
/// let y = rng.sample::<u16, _>(Uniform::new(10, 15));
377380
/// ```
378381
fn sample<T, D: Distribution<T>>(&mut self, distr: D) -> T {
379382
distr.sample(self)

0 commit comments

Comments
 (0)