Skip to content

Commit 57fbc71

Browse files
committed
Add OsRng examples
Fix rand_os example
1 parent 8fa74fc commit 57fbc71

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

rand_os/src/lib.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,6 @@
99

1010
//! Interface to the random number generator of the operating system.
1111
//!
12-
//! # Usage example
13-
//! ```
14-
//! use rand_os::{OsRng, rand_core::RngCore};
15-
//!
16-
//! let mut key = [0u8; 16];
17-
//! OsRng.fill_bytes(&mut key);
18-
//! let random_u64 = OsRng.next_u64();
19-
//! ```
20-
//!
2112
//! # Blocking and error handling
2213
//!
2314
//! It is possible that when used during early boot the first call to `OsRng`
@@ -54,6 +45,15 @@ use rand_core::{CryptoRng, RngCore, Error, ErrorKind, impls};
5445
/// The implementation is provided by the [getrandom] crate. Refer to
5546
/// [getrandom] documentation for details.
5647
///
48+
/// # Usage example
49+
/// ```
50+
/// use rand_os::{OsRng, rand_core::RngCore};
51+
///
52+
/// let mut key = [0u8; 16];
53+
/// OsRng.fill_bytes(&mut key);
54+
/// let random_u64 = OsRng.next_u64();
55+
/// ```
56+
///
5757
/// [getrandom]: https://crates.io/crates/getrandom
5858
#[derive(Clone, Copy, Debug)]
5959
pub struct OsRng;

src/rngs/os.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ use rand_core::{CryptoRng, RngCore, Error, ErrorKind, impls};
1919
/// The implementation is provided by the [getrandom] crate. Refer to
2020
/// [getrandom] documentation for details.
2121
///
22+
/// ## Example
23+
///
24+
/// ```
25+
/// use rand::rngs::{StdRng, OsRng};
26+
/// use rand::{RngCore, SeedableRng};
27+
///
28+
/// println!("Random number, straight from the OS: {}", OsRng.next_u32());
29+
/// let mut rng = StdRng::from_rng(OsRng).unwrap();
30+
/// println!("Another random number: {}", rng.next_u32());
31+
/// ```
32+
///
2233
/// [getrandom]: https://crates.io/crates/getrandom
2334
#[derive(Clone, Copy, Debug)]
2435
pub struct OsRng;

0 commit comments

Comments
 (0)