Skip to main content

random_string

Function random_string 

Source
pub fn random_string(len: usize, charset: &[u8]) -> String
๐Ÿ‘ŽDeprecated since 1.1.0:

predictable: never use for tokens or passwords. Use tier3::random_string (returns io::Result) for secrets, or unique_name / unique_hex for IDs

Available on crate feature tier2 only.
Expand description

Generate a random string of exactly len characters drawn from charset (uniformly).

Selection is unbiased โ€” bounded_u64(charset.len() as u64) is used internally, so the per-character distribution is uniform.

ยงPanics

Panics if charset is empty or contains a non-ASCII byte (byte >= 128). The ASCII guard preserves UTF-8 validity of the returned String.

Deprecated: predictable output. This is not a token or a password generator. Use tier3::random_string for anything that must be hard to guess.

ยงExample

use mod_rand::{tier2, charsets};
let s = tier2::random_string(16, charsets::ALPHANUMERIC);
assert_eq!(s.len(), 16);