我正在尝试使用web_sys
条板箱填充带有随机数的向量
类似这样的东西:
extern crate web_sys;
use web_sys::Crypto;
// ...
fn main() {
// ...
let mut arry = [0u8; 128];
let crypto = Crypto::new();
crypto.get_random_values_with_u8_array(&mut arry)
// ...
}
然而,Crypto
结构并未实现new
方法,并且文档也未提及实例化该结构所需的字段。
我已将Crypto
作为Cargo.toml
文件中的功能:
[dependencies.web-sys]
version = "0.3"
features = [
"Crypto"
]
这是相关的文档页面:
https://docs.rs/web-sys/0.3.35/i686-unknown-linux-gnu/web_sys/struct.Crypto.html
有人有使用该软件的经验吗?
答案 0 :(得分:1)
感谢Masklinn的评论,我能够通过获取Window实例并在其上调用crypto
来使其正常工作。
let mut rand_arry = [0u8; 128];
let window_instance = web_sys::window().unwrap();
let crypto = window_instance.crypto.unwrap();
crypto.get_random_values_with_u8_array(&mut rand_arry).unwrap();
确保将Crypto
和Window
声明为网络系统功能
[dependencies.web-sys]
version = "0.3"
features = [
"Window",
"Crypto"
]