secure
import std::random::secure; · source
Types
SecureRng
type struct SecureRng {
static new() -> SecureRng;
try_fill(&this, dst: Slice<u8>) -> Result<(), RandomError>;
}
import std::random::secure;
mut s: SecureRng = SecureRng::new();
s.fill(key.as_slice());
Because it satisfies RandomSource, the full draw surface above is available. Those defaults panic if the kernel cannot supply entropy — a broken system invariant rather than an expected failure. When you need to handle it, use try_fill, which reports it as EntropyUnavailable.
SecureRng is stateless, so a copy is the same source. The backing call is per target: getrandom on Unix, RtlGenRandom on Windows. Rng::from_os routes through the same portable helper rather than binding the Linux-only symbol directly.
Constructors
static new() -> SecureRng;
static new() -> SecureRng;A handle to the kernel CSPRNG. Holds no state.
Methods
try_fill(&this, dst: Slice<u8>) -> Result<(), RandomError>;
try_fill(&this, dst: Slice<u8>) -> Result<(), RandomError>;Fill dst with secure bytes, reporting EntropyUnavailable if the kernel source cannot satisfy the request. The careful counterpart to the RandomSource::fill default (which panics on failure). On Ok the whole view is written.
Trait implementations
implement trait RandomSource for struct SecureRng
Its RandomSource impl overrides fill so a bulk request is one kernel call rather than a loop of next_64.
type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct SecureRng {}Cryptographically secure source backed by the kernel CSPRNG. Slower than Rng; use it only where unpredictability matters.
static name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
static SecureRng::new() -> SecureRngA handle to the kernel CSPRNG. Holds no state.
type struct SecureRng {}Cryptographically secure source backed by the kernel CSPRNG. Slower than Rng; use it only where unpredictability matters.
SecureRng::try_fill(&this, dst: Slice<u8>) -> Result<(), RandomError>Fill dst with secure bytes, reporting EntropyUnavailable if the kernel source cannot satisfy the request. The careful counterpart to the RandomSource::fill default (which panics on failure). On Ok the whole view is written.
&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
(parameter) dst: Slice<u8>type struct Slice<T> {
ptr: T*;
length: u64;
}Slice<T>: a borrowed view into a contiguous sequence.
std::core::sliceprimitive ()The unit type: a value that carries no information, and the empty tuple. Distinct from void, which is the absence of a value - Result<(), E> succeeds with nothing to return. 0 bytes.
type enum RandomError {
EntropyUnavailable;
EmptyRange;
InvalidWeights;
}What went wrong in a fallible random operation.
std::random::errorimport a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
mut name: Type = value;A mutable binding, reassignable after declaration - mutability is opt-in, const is the default. At module level it declares mutable global state. Before & it marks an exclusive reference or receiver instead.
mut s: SecureRngstatic SecureRng::new() -> SecureRngA handle to the kernel CSPRNG. Holds no state.
std::random::securemut s: SecureRngSecureRng::fill(mut &this, dst: Slice<u8>) -> voidvia trait RandomSource
Override the byte-fill default to pull straight from the kernel in one pass rather than synthesizing bytes from next_64.
type trait RandomSource {
next_64(mut &this) -> u64;
next<T>(mut &this) -> T;
next_below(mut &this, bound: u64) -> u64;
next_range<T>(mut &this, lo: T, hi: T) -> T;
next_below_wide(mut &this, bound: u128) -> u128;
fill(mut &this, dst: Slice<u8>) -> void;
shuffle<T>(mut &this, items: &Slice<T>) -> void;
choose<T>(mut &this, items: &Slice<T>) -> Option<T*>;
}SecureRng::try_fill(&this, dst: Slice<u8>) -> Result<(), RandomError>Fill dst with secure bytes, reporting EntropyUnavailable if the kernel source cannot satisfy the request. The careful counterpart to the RandomSource::fill default (which panics on failure). On Ok the whole view is written.
RandomError::EntropyUnavailabletype struct Rng {
s0: u64;
s1: u64;
s2: u64;
s3: u64;
}A fast non-cryptographic PRNG (xoshiro256**). The 256-bit state is the four words s0..s3.
static Rng::from_os() -> RngSeed from the OS CSPRNG. Falls back to a monotonic-clock + pid mix if the kernel source is briefly unavailable, so it never hard-fails.
std::random::rngRandomSource::fill(mut &this, dst: Slice<u8>) -> voidFill dst with random bytes, writing through the view.
Result::Ok(T)implement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
implement trait Trait for Type { ... }Introduces a trait implementation: the block supplies, for one concrete type, the method bodies the trait requires.
language referenceimplement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language reference