Huy's Notes
Cloudflare Workers with Rust

Cloudflare Workers with Rust

#rust #webassembly

Wrangler is Cloudflare's solution for writing, testing and deploying Worker Scripts right on your local machine. It supports many project templates, including WebAssembly in Rust.

To start a new project, type:

wrangler generate your-project <template-url>

Then you can run the following command to have Wrangler compile and hook up everything for you, that's including compiling your Rust module into a Wasm package and loading it up in a JavaScript worker.

wrangler preview --watch

Wrangler uses wasm-bindgen as a solution to export your Rust data into JavaScript. You'll need to make sure all the crates you're using supports the wasm32-unknown-unknown target.


One of the thing I found after played around with Wrangler is, it's pretty hard to debug the error in your Rust code when running as a Wasm package. Yes, this isn't a Wrangler thing but a Wasm thing.

For example, when a panic happen, you'll get this useless error message in your JavaScript console:

Runtime Error: unreachable

A Wrangler project usually have console_error_panic_hook integrated, it's a crate that help you see a more detailed Rust error message, when it fails inside a Wasm package. To enable, just call utils::set_panic_hook(); where you need, for example:

#[wasm_bindgen]
pub fn parse(content: &str) -> String {
+    utils::set_panic_hook();
...
 }

Referred in


If you think this note resonated, be it positive or negative, please feel free to send me an email and we can talk.