Skip to content

Commit 301105d

Browse files
committed
Update documentation on Rng and RngCore
1 parent 6508ae2 commit 301105d

File tree

1 file changed

+39
-14
lines changed

1 file changed

+39
-14
lines changed

src/lib.rs

+39-14
Original file line numberDiff line numberDiff line change
@@ -354,18 +354,37 @@ pub trait Rand : Sized {
354354
fn rand<R: Rng>(rng: &mut R) -> Self;
355355
}
356356

357-
/// A random number generator.
357+
/// The core of a random number generator.
358358
///
359359
/// This trait encapsulates the low-level functionality common to all
360360
/// generators, and is the "back end", to be implemented by generators.
361+
/// End users should normally use [`Rng`] instead.
362+
///
363+
/// Unlike [`Rng`], this trait is object-safe. To use a type-erased [`Rng`] —
364+
/// i.e. dynamic dispatch — this trait must be used, along with [`Rng`] to
365+
/// use its generic functions:
366+
///
367+
/// ```
368+
/// use rand::{Rng, RngCore};
369+
///
370+
/// fn use_rng(mut rng: &mut RngCore) -> u32 {
371+
/// rng.gen_range(1, 7)
372+
/// }
373+
///
374+
/// // or:
375+
/// fn use_any_rng<R: RngCore>(rng: &mut R) -> char {
376+
/// // TODO: generating a single char should be easier than this
377+
/// rng.gen_ascii_chars().next().unwrap()
378+
/// }
379+
/// ```
380+
///
361381
/// Several extension traits exist:
362382
///
363-
/// * [`Rng`] provides high-level generic functionality built on top of
364-
/// `RngCore`
365-
/// * [`SeedableRng`] is another "back end" trait covering creation and
366-
/// seeding of algorithmic RNGs (PRNGs)
367-
/// * [`NewRng`] is a high-level trait providing a convenient way to create
368-
/// freshly-seeded PRNGs
383+
/// * [`Rng`] provides high-level functionality using generic functions
384+
/// * [`SeedableRng`] is another low-level trait to be implemented by PRNGs
385+
/// (algorithmic RNGs), concerning creation and seeding
386+
/// * [`NewRng`] is a high-level trait providing a `new()` function, allowing
387+
/// easy construction of freshly-seeded PRNGs
369388
///
370389
/// [`Rng`]: trait.Rng.html
371390
/// [`SeedableRng`]: trait.SeedableRng.html
@@ -502,14 +521,20 @@ pub trait RngCore {
502521
/// An automatically-implemented extension trait on [`RngCore`] providing high-level
503522
/// generic methods for sampling values and other convenience methods.
504523
///
505-
/// Users should "use" this trait to enable its extension methods on [`RngCore`]
506-
/// or require this type directly (i.e. `<R: Rng>`). Since `Rng`
507-
/// extends `RngCore` and every `RngCore` implements `Rng`, usage of the two
508-
/// traits is somewhat interchangeable.
524+
/// This is the primary trait to use when generating random values. Example:
525+
///
526+
/// ```
527+
/// use rand::Rng;
528+
///
529+
/// fn use_rng<R: Rng>(rng: &mut R) -> f32 {
530+
/// rng.gen()
531+
/// }
532+
/// ```
509533
///
510-
/// This functionality is provided as an extension trait to allow separation
511-
/// between the backend (the [`RngCore`] providing randomness) and the front-end
512-
/// (converting that randomness to the desired type and distribution).
534+
/// Since this trait exclusively uses generic methods, it is marked `Sized`.
535+
/// Should it be necessary to support trait objects, use [`RngCore`].
536+
/// Since `Rng` extends `RngCore` and every `RngCore` implements `Rng`, usage
537+
/// of the two traits is somewhat interchangeable.
513538
///
514539
/// [`RngCore`]: trait.RngCore.html
515540
pub trait Rng: RngCore + Sized {

0 commit comments

Comments
 (0)