I want to get random values in wasm

Asked 1 years ago, Updated 1 years ago, 123 views

I get an error when I compile into WebAssembly in Rust.

//rust
external rate rand;
userland::Rng;

US>fnmain(){
    let secret_number = land::thread_rng().gen_range(1,101);
    secret_number;
}
//toml
[package]
name = "main"
version = "0.1.0"
authors=["akats" ]
edition="2018"

[Dependencies]
land="*"

When you compile this with rustc main.rs, you get the following error:

 or [E0464]: multiple matching records for `land`
 -->main.rs:1:1
  |
1 | external rate rand;
  | ^^^^^^^^^^^^^^^^^^
  |
  = note:candidates:
          crate `land`:\\?\C:\Users\akats\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\x86_64-pc-windows-msvc\lib\lib\librand-453efbf1160c222c.rlib
          crate `land`:\\?\C:\Users\akats\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\x86_64-pc-windows-msvc\lib\lib\librand-9c0cd53839437cf2.rlib
          crate `land`:\\?\C:\Users\akats\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\x86_64-pc-windows-msvc\lib\lib\librand-6d48c3 cad20b5526.rlib

error [E0463]:can't find crate for `land`
 -->main.rs:1:1
  |
1 | external rate rand;
  | ^^^^^^^^^^^^^^^^^^ can't find crate

errors:aborting due to 2 previous errors

Some errors occurred: E0463, E0464.
For more information about an error, try `rustc--explain E0463`.

It works well if I output it with Rust only, but I don't know what's wrong.Thank you for your cooperation.

rust webassembly

2022-09-30 18:17

2 Answers

In addition to what Termoshtt is saying, you should be able to generate the .wasm file by adding the following to Cargo.toml.

 [lib]
crate-type=["cdylib"]

If you are concerned about the role of this description, you may want to visit The Manifest Format.

However, the land crate should still not work.
This is because WebAssembly has a specification that is defined independently of the Web and has only simple computational instructions, but wasm-unknown-unknown is a target for that pure wasm, so there is no way to supply random number seeds.
JS has a random number related API, so if you try to supply random number seeds from it, you usually rely on some tool to bridge JS and wasm because the import of JS functions into wasm is not obvious, but of course, the import mechanism differs from tool to tool, and you can't set any of them as defaults.
(The MDN document is helpful.)

Therefore, you can think of the following methods:

I recommend a couple of them.
When 1 is initialized as a constant, it naturally returns random numbers in a fixed order every time it is executed.
You can get random number seeds by importing JS functions on your own, but it's troublesome and I'm into it.
If you still want to do it, please refer to the following link.
Explanation of tool-less wasm development, Explanation of import, Methods for obtaining random numbers in JS

2, 3 is a method of supplying random number seeds from a JavaScript function, for example, 2 is to write code using the wasm-bindgen with the wasm-bindgen feature enabled in the rand crate.
I've only used wasm-bindgen, but I'll recommend 2 more just in case.
wasm-bindgen is useful on the pages mentioned by termoshtt and Rust and WebAssembly.

4 seems to be a good option in the future, but I can't use it yet.


2022-09-30 18:17

The land is an external crate, not a standard library (libstd) included in the Rust body, so you basically need to use cargo instead of trustc to download, build, and link it.

cargo build --target wasm32-unknown-unknown

If so, target/wasm-unknown-unknown/debug and below should have a file with a wasm extension.

For cooperation with npm and use of wasm-bindgen, I think the following documents from Mozilla will be helpful (although the Japanese translation is only halfway through).
https://developer.mozilla.org/ja/docs/WebAssembly/rust_to_wasm


2022-09-30 18:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.