1

How could I generate an array of random strings in Rust?

fn random_string_arr(n_strings: i32, max_str_length: i32, min_str_length: i32) -> Vec<String>{
   ///generate array of random ASCII strings
}
ANimator120
  • 2,556
  • 1
  • 20
  • 52
  • 3
    Does this answer your question? [How do I create a random String by sampling from alphanumeric characters?](https://stackoverflow.com/questions/54275459/how-do-i-create-a-random-string-by-sampling-from-alphanumeric-characters) – Ibraheem Ahmed Dec 25 '20 at 02:51

1 Answers1

0

This is what I ended up with:

use rand::{distributions::Alphanumeric, Rng};

fn random_string_arr(n_strings: i32, min_str_length: i32, max_str_length: i32) -> Vec<String>{
    let mut string_arr = vec![];
    
    for x in 0..n_strings{
        let mut count =  rand::thread_rng().gen_range(min_str_length..max_str_length);
        println!("rand char count is:{}", count.to_string());
        let s: String = rand::thread_rng()
            .sample_iter(&Alphanumeric)
            .take(count)
            .map(char::from)
            .collect();

        string_arr.push(s)
    }
    string_arr
 }
Binier
  • 1,047
  • 2
  • 11
  • 25
ANimator120
  • 2,556
  • 1
  • 20
  • 52