Worker + Rust + Random Value Generator

I love Cloudflare! Thank you all!

When I add the rand crate to my cargo.toml file like

rand = "0.8.4"

Or

rand = "0.8.5"

The following error is produced @ the next wrangler dev

$ wrangler dev
šŸŒ€  Running cargo install -q worker-build && worker-build --release
[INFO]: šŸŽÆ  Checking for the Wasm target...
[INFO]: šŸŒ€  Compiling to Wasm...
   Compiling getrandom v0.2.6
error: the wasm32-unknown-unknown target is not supported by default, you may need to enable the "js" feature. For more information see: https://docs.rs/getrandom/#webassembly-support
   --> /Users/chris-carrington/.cargo/registry/src/github.com-1ecc6299db9ec823/getrandom-0.2.6/src/lib.rs:235:9
    |
235 | /         compile_error!("the wasm32-unknown-unknown target is not supported by \
236 | |                         default, you may need to enable the \"js\" feature. \
237 | |                         For more information see: \
238 | |                         https://docs.rs/getrandom/#webassembly-support");
    | |________________________________________________________________________^

error[E0433]: failed to resolve: use of undeclared crate or module `imp`
   --> /Users/chris-carrington/.cargo/registry/src/github.com-1ecc6299db9ec823/getrandom-0.2.6/src/lib.rs:262:5
    |
262 |     imp::getrandom_inner(dest)
    |     ^^^ use of undeclared crate or module `imp`

For more information about this error, try `rustc --explain E0433`.
error: could not compile `getrandom` due to 2 previous errors
Error: Compiling your crate to WebAssembly failed
Caused by: failed to execute `cargo build`: exited with exit status: 101
  full command: "cargo" "build" "--lib" "--release" "--target" "wasm32-unknown-unknown"
Error: wasm-pack exited with status exit status: 1
Error: Build failed! Status Code: 1

Here is the link in the error

Anyone know how to dodge this error and gain random functionality in our Rust, Cloudflare Workers please?!

Thank you so much!

Have you done this in your cargo.toml?

1 Like

Hi @KianNH! Thank you!

This works! :call_me_hand:

[dependencies]
rand = "0.8.5"
getrandom = { version = "0.2", features = ["js"] }
1 Like