【发布时间】:2021-03-25 17:53:42
【问题描述】:
我正在实现一个模糊器,我想生成随机的 unicode 字符串。我想出了这个解决方案,但是,它的效率非常低,并且很少产生一些字符串。有没有更好的方法来生成 unicode 字符串?
谢谢。
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(())
}
【问题讨论】:
-
不,因为它只从字母数字字符创建字符串。我想生成包含任何 unicode 字符的字符串。