2023-05-27 08:37:34 +02:00
|
|
|
use rand::{distributions::Alphanumeric, thread_rng, Rng};
|
|
|
|
|
|
|
|
pub fn gen_string(length: u16) -> String {
|
|
|
|
thread_rng()
|
|
|
|
.sample_iter(Alphanumeric)
|
|
|
|
.take(length.into())
|
|
|
|
.map(char::from)
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use std::thread;
|
|
|
|
|
|
|
|
use super::gen_string;
|
|
|
|
|
|
|
|
#[test]
|
2023-05-27 11:50:07 +02:00
|
|
|
fn unit_random_can_generate_string() {
|
2023-05-27 08:37:34 +02:00
|
|
|
assert_eq!(gen_string(16).len(), 16);
|
|
|
|
assert_ne!(gen_string(16), gen_string(16));
|
|
|
|
let s1 = thread::spawn(|| gen_string(16));
|
|
|
|
let s2 = thread::spawn(|| gen_string(16));
|
|
|
|
assert_ne!(s1.join().unwrap(), s2.join().unwrap());
|
|
|
|
}
|
|
|
|
}
|