How can I generate random string in Rust?
If you want a sequence of printable ASCII characters, you can usegen_ascii_chars
function.If you want a sequence of random bytes (like the C++ code you've written), you can use
gen_iter::
function.If you want a sequence of random Unicode code points (strictly speaking, Unicode Scalar Values), then
gen_iter::
function.Example of generating random string in Rust
#![allow(unstable)]
use std::rand::{self, Rng};
fn main() {
let s = rand::thread_rng()
.gen_ascii_chars()
.take(10)
.collect::<String>();
println!("random string: {}", s);
}