@@ -354,18 +354,37 @@ pub trait Rand : Sized {
354
354
fn rand < R : Rng > ( rng : & mut R ) -> Self ;
355
355
}
356
356
357
- /// A random number generator.
357
+ /// The core of a random number generator.
358
358
///
359
359
/// This trait encapsulates the low-level functionality common to all
360
360
/// 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
+ ///
361
381
/// Several extension traits exist:
362
382
///
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
369
388
///
370
389
/// [`Rng`]: trait.Rng.html
371
390
/// [`SeedableRng`]: trait.SeedableRng.html
@@ -502,14 +521,20 @@ pub trait RngCore {
502
521
/// An automatically-implemented extension trait on [`RngCore`] providing high-level
503
522
/// generic methods for sampling values and other convenience methods.
504
523
///
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
+ /// ```
509
533
///
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.
513
538
///
514
539
/// [`RngCore`]: trait.RngCore.html
515
540
pub trait Rng : RngCore + Sized {
0 commit comments