1

I'm implementing a fuzzer and I'd like to generate random unicode strings. I came up with this solution, however, it's very inefficient and seldomly produces some string. Is there a better way to generate unicode strings?

Thank you.

use rand::{thread_rng, Error, Rng};
use std::convert::TryFrom;

fn main() -> Result<(), Error> {
    let mut rng = thread_rng();
    let mut arr: Vec<u32> = vec![0; 1024];
    rng.try_fill(&mut arr[..])?;

    println!(
        "{:?}",
        arr.iter()
            .map(|u| char::try_from(*u))
            .flatten()
            .collect::<String>()
    );

    Ok(())
}

Rust Playground link

matusf
  • 469
  • 1
  • 8
  • 20
  • 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) – vallentin Mar 25 '21 at 18:01
  • No since, it creates just strings from alphanumeric characters. I'd like to generate string containing any unicode character. – matusf Mar 25 '21 at 18:04

1 Answers1

5

Use something like this:

fn get_random_string(len: usize) -> String {
    rand::thread_rng()
        .sample_iter::<char, _>(rand::distributions::Standard)
        .take(len)
        .collect()
}

Playground

You might want to filter control characters, if you want to print it. Also the character distribution is very unlike any UTF-8 strings you might encounter in the wild.

HHK
  • 4,852
  • 1
  • 23
  • 40
  • 4
    In case anyone is curious, the secret sauce behind `sample_iter` is the [implementation of the `Distribution` trait](https://github.com/rust-random/rand/blob/9555338a270f408913068940f768a312561491f2/src/distributions/other.rs#L67) for the `Standard` distribution type. – user4815162342 Mar 25 '21 at 18:58