random_alphanumeric
A random string of letters and digits (A-Za-z0-9) from the operating system’s random
source, with every character equally likely.
Needs the random-entropy feature. Bytes that would favor some characters are discarded
rather than folded with a modulo (256 is not a multiple of 62), so a 20-character result
carries 20 × log2(62) ≈ 119 bits of randomness.
Import
Section titled “Import”Cargo feature random (enabled by default). To compile only this module:
or in Cargo.toml:
Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
len | usize | The number of characters. |
Returns
Section titled “Returns”Result<String, EntropyError> — A string of len characters, or an EntropyError.
Errors
Section titled “Errors”Returns an EntropyError when the operating system cannot provide random bytes.
Examples
Section titled “Examples”Error type: EntropyError
Section titled “Error type: EntropyError”The operating system could not provide random bytes.
Rare, but possible (no entropy source at boot, a sandbox that blocks the system call, an unsupported platform). The functions that return it never fall back to weaker randomness: a key or a token made from predictable bytes is worse than no key.
