pub struct OsRng;
Expand description
An interface over the operating-system’s random data source
This is a zero-sized struct. It can be freely constructed with just OsRng
.
The implementation is provided by the getrandom crate. Refer to getrandom documentation for details.
This struct is available as rand_core::OsRng
and as rand::rngs::OsRng
.
In both cases, this requires the crate feature os_rng
or std
(enabled by default in rand
but not in rand_core
).
§Blocking and error handling
It is possible that when used during early boot the first call to OsRng
will block until the system’s RNG is initialised. It is also possible
(though highly unlikely) for OsRng
to fail on some platforms, most
likely due to system mis-configuration.
After the first successful call, it is highly unlikely that failures or significant delays will occur (although performance should be expected to be much slower than a user-space PRNG).
§Usage example
use rand_core::{TryRngCore, OsRng};
let mut key = [0u8; 16];
OsRng.try_fill_bytes(&mut key).unwrap();
let random_u64 = OsRng.try_next_u64().unwrap();
Trait Implementations§
Source§impl TryRngCore for OsRng
impl TryRngCore for OsRng
Source§fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Self::Error>
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Self::Error>
dest
entirely with random data.Source§fn read_adapter(&mut self) -> RngReadAdapter<'_, Self> ⓘwhere
Self: Sized,
fn read_adapter(&mut self) -> RngReadAdapter<'_, Self> ⓘwhere
Self: Sized,
RngCore
to a RngReadAdapter
.