Skip to content

Commit fd0bd08

Browse files
authored
Merge pull request #304 from dhardy/new
Improve doc for NewRng::new alternative
2 parents 1caee2a + 09599bc commit fd0bd08

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/lib.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -768,10 +768,25 @@ pub trait NewRng: SeedableRng {
768768
///
769769
/// Normally this will use `OsRng`, but if that fails `JitterRng` will be
770770
/// used instead. Both should be suitable for cryptography. It is possible
771-
/// that both entropy sources will fail though unlikely.
771+
/// that both entropy sources will fail though unlikely; failures would
772+
/// almost certainly be platform limitations or build issues, i.e. most
773+
/// applications targetting PC/mobile platforms should not need to worry
774+
/// about this failing.
772775
///
773-
/// Panics on error. If error handling is desired, use
774-
/// `RNG::from_rng(&mut EntropyRng::new())` instead.
776+
/// If all entropy sources fail this will panic. If you need to handle
777+
/// errors, use the following code, equivalent aside from error handling:
778+
///
779+
/// ```rust
780+
/// use rand::{Rng, StdRng, EntropyRng, SeedableRng, Error};
781+
///
782+
/// fn foo() -> Result<(), Error> {
783+
/// // This uses StdRng, but is valid for any R: SeedableRng
784+
/// let mut rng = StdRng::from_rng(&mut EntropyRng::new())?;
785+
///
786+
/// println!("random number: {}", rng.gen_range(1, 10));
787+
/// Ok(())
788+
/// }
789+
/// ```
775790
fn new() -> Self;
776791
}
777792

0 commit comments

Comments
 (0)