|
53 | 53 | //! to seed from a strong parent generator with [`from_rng`]:
|
54 | 54 | //!
|
55 | 55 | //! ```
|
| 56 | +//! # use rand::{Rng, Error}; |
56 | 57 | //! // seed with fresh entropy:
|
57 | 58 | //! use rand::{StdRng, NewRng};
|
58 | 59 | //! let mut rng = StdRng::new();
|
| 60 | +//! # let v: u32 = rng.gen(); |
59 | 61 | //!
|
60 | 62 | //! // seed from thread_rng:
|
61 | 63 | //! use rand::{SmallRng, SeedableRng, thread_rng};
|
62 |
| -//! let mut rng = SmallRng::from_rng(thread_rng()); |
| 64 | +//! |
| 65 | +//! # fn try_inner() -> Result<(), Error> { |
| 66 | +//! let mut rng = SmallRng::from_rng(thread_rng())?; |
| 67 | +//! # let v: u32 = rng.gen(); |
| 68 | +//! # Ok(()) |
| 69 | +//! # } |
| 70 | +//! # try_inner().unwrap() |
63 | 71 | //! ```
|
64 | 72 | //!
|
65 | 73 | //! In case you specifically want to have a reproducible stream of "random"
|
|
175 | 183 | html_root_url = "https://docs.rs/rand/0.5")]
|
176 | 184 |
|
177 | 185 | #![deny(missing_debug_implementations)]
|
| 186 | +#![doc(test(attr(allow(unused_variables), deny(warnings))))] |
178 | 187 |
|
179 | 188 | #![cfg_attr(not(feature="std"), no_std)]
|
180 | 189 | #![cfg_attr(all(feature="alloc", not(feature="std")), feature(alloc))]
|
@@ -292,11 +301,14 @@ pub trait Rand : Sized {
|
292 | 301 | /// Example:
|
293 | 302 | ///
|
294 | 303 | /// ```rust
|
| 304 | +/// # use rand::thread_rng; |
295 | 305 | /// use rand::Rng;
|
296 | 306 | ///
|
297 | 307 | /// fn foo<R: Rng + ?Sized>(rng: &mut R) -> f32 {
|
298 | 308 | /// rng.gen()
|
299 | 309 | /// }
|
| 310 | +/// |
| 311 | +/// # let v = foo(&mut thread_rng()); |
300 | 312 | /// ```
|
301 | 313 | ///
|
302 | 314 | /// # Iteration
|
@@ -345,7 +357,7 @@ pub trait Rng: RngCore {
|
345 | 357 | /// use rand::{thread_rng, Rng};
|
346 | 358 | ///
|
347 | 359 | /// let mut arr = [0i8; 20];
|
348 |
| - /// thread_rng().try_fill(&mut arr[..]); |
| 360 | + /// thread_rng().fill(&mut arr[..]); |
349 | 361 | /// ```
|
350 | 362 | ///
|
351 | 363 | /// [`fill_bytes`]: https://docs.rs/rand_core/0.1/rand_core/trait.RngCore.html#method.fill_bytes
|
@@ -433,6 +445,7 @@ pub trait Rng: RngCore {
|
433 | 445 | /// # Example
|
434 | 446 | ///
|
435 | 447 | /// ```
|
| 448 | + /// # #![allow(deprecated)] |
436 | 449 | /// use rand::{thread_rng, Rng};
|
437 | 450 | ///
|
438 | 451 | /// let mut rng = thread_rng();
|
@@ -480,7 +493,7 @@ pub trait Rng: RngCore {
|
480 | 493 | /// # Example
|
481 | 494 | ///
|
482 | 495 | /// ```rust
|
483 |
| - /// #[allow(deprecated)] |
| 496 | + /// # #![allow(deprecated)] |
484 | 497 | /// use rand::{thread_rng, Rng};
|
485 | 498 | ///
|
486 | 499 | /// let mut rng = thread_rng();
|
@@ -520,7 +533,7 @@ pub trait Rng: RngCore {
|
520 | 533 | /// # Example
|
521 | 534 | ///
|
522 | 535 | /// ```rust
|
523 |
| - /// #[allow(deprecated)] |
| 536 | + /// # #![allow(deprecated)] |
524 | 537 | /// use rand::{thread_rng, Rng};
|
525 | 538 | ///
|
526 | 539 | /// let s: String = thread_rng().gen_ascii_chars().take(10).collect();
|
@@ -750,20 +763,23 @@ pub trait NewRng: SeedableRng {
|
750 | 763 | /// almost certainly be platform limitations or build issues, i.e. most
|
751 | 764 | /// applications targetting PC/mobile platforms should not need to worry
|
752 | 765 | /// about this failing.
|
753 |
| - /// |
| 766 | + /// |
754 | 767 | /// If all entropy sources fail this will panic. If you need to handle
|
755 | 768 | /// errors, use the following code, equivalent aside from error handling:
|
756 |
| - /// |
| 769 | + /// |
757 | 770 | /// ```rust
|
758 |
| - /// use rand::{Rng, StdRng, EntropyRng, SeedableRng, Error}; |
759 |
| - /// |
760 |
| - /// fn foo() -> Result<(), Error> { |
761 |
| - /// // This uses StdRng, but is valid for any R: SeedableRng |
762 |
| - /// let mut rng = StdRng::from_rng(EntropyRng::new())?; |
763 |
| - /// |
764 |
| - /// println!("random number: {}", rng.gen_range(1, 10)); |
765 |
| - /// Ok(()) |
766 |
| - /// } |
| 771 | + /// # use rand::Error; |
| 772 | + /// use rand::{Rng, StdRng, EntropyRng, SeedableRng}; |
| 773 | + /// |
| 774 | + /// # fn try_inner() -> Result<(), Error> { |
| 775 | + /// // This uses StdRng, but is valid for any R: SeedableRng |
| 776 | + /// let mut rng = StdRng::from_rng(EntropyRng::new())?; |
| 777 | + /// |
| 778 | + /// println!("random number: {}", rng.gen_range(1, 10)); |
| 779 | + /// # Ok(()) |
| 780 | + /// # } |
| 781 | + /// |
| 782 | + /// # try_inner().unwrap() |
767 | 783 | /// ```
|
768 | 784 | fn new() -> Self;
|
769 | 785 | }
|
@@ -844,11 +860,13 @@ impl CryptoRng for StdRng {}
|
844 | 860 | /// Initializing `StdRng` with a random seed can be done using `NewRng`:
|
845 | 861 | ///
|
846 | 862 | /// ```
|
| 863 | +/// # use rand::Rng; |
847 | 864 | /// use rand::{NewRng, SmallRng};
|
848 | 865 | ///
|
849 | 866 | /// // Create small, cheap to initialize and fast RNG with a random seed.
|
850 | 867 | /// // The randomness is supplied by the operating system.
|
851 | 868 | /// let mut small_rng = SmallRng::new();
|
| 869 | +/// # let v: u32 = small_rng.gen(); |
852 | 870 | /// ```
|
853 | 871 | ///
|
854 | 872 | /// When initializing a lot of `SmallRng`, using `thread_rng` can be more
|
@@ -930,6 +948,7 @@ pub fn weak_rng() -> XorShiftRng {
|
930 | 948 | /// # Example
|
931 | 949 | ///
|
932 | 950 | /// ```rust
|
| 951 | +/// # #![allow(deprecated)] |
933 | 952 | /// use rand::{thread_rng, sample};
|
934 | 953 | ///
|
935 | 954 | /// let mut rng = thread_rng();
|
|
0 commit comments