How to generate a random fixed size string in Rust
For generate a random string with fixed size in Rust, we can use Alphanumeric trait from rand crate:
Alphanumeric
rand
use rand::{distributions::Alphanumeric, Rng}; // 0.8 fn main() { let s: String = rand::thread_rng() .sample_iter(&Alphanumeric) .take(7) .map(char::from) .collect(); println!("{}", s); }
ref: https://stackoverflow.com/questions/54275459/how-do-i-create-a-random-string-by-sampling-from-alphanumeric-characters