[][src]Function artichoke_backend::extn::stdlib::securerandom::alphanumeric

pub fn alphanumeric(len: Option<i64>) -> Result<Vec<u8, Global>, ArgumentError>

Generate a random sequence of ASCII alphanumeric bytes.

If len is Some and non-negative, generate a String of len random ASCII alphanumeric bytes. If len is None, generate 16 random alphanumeric bytes.

The returned Vec<u8> is guaranteed to contain only ASCII bytes.

Examples

let bytes = spinoso_securerandom::alphanumeric(Some(1024))?;
let bytes = String::from_utf8(bytes)?;
assert_eq!(bytes.len(), 1024);
assert!(bytes.is_ascii());
assert!(bytes.find(|ch: char| !ch.is_ascii_alphanumeric()).is_none());

Errors

If the given length is negative, return an ArgumentError.

If the underlying source of randomness returns an error, return a RandomBytesError.