WebAssembly Modules in Rust: Syntax Deep Dive

Exploring features, compilation and dependencies of a Rust wasm module

Ross Bulat


This is a continuation of the previous article that introduced developing WebAssembly modules in Rust, where the demo project (hosted on Github) showcases an asynchronous fetch request called directly within Rust code and the compiled wasm binary.

Where the introductory article covered the process of developing and building a WebAssembly module, here we will explore the syntax of the project in more detail to understand how the project is constructed in Rust. The concepts covered here will surely be helpful if you are a web developer exploring the Rust programming language for use with WebAssembly modules, or studying Rust use cases in general.

More specifically, we will visit concepts including:

  • How Cargo and conditional compilation have been used to include a WebAssembly specific debugging tool for the browser
  • How the use of FFI (Foreign Function Interfaces) are used to link Javascript and Rust functions
  • How Rust deals with asynchronous functions and Javascript Promise objects with Rust Futures and utilities from web-sys and js-sys crates.

Be sure to refer to the previous article for a more generalised introduction of WebAssembly modules in Rust, including the compiling and publishing process for use in a React project.

Fetch request WebAssembly demo on Github

The project referenced throughout this article can be found here on Github. Compile the module for testing, or run the included React project to see Javascript interacting with the compiled WebAssembly module included.

Cargo and Conditional Compilation

Conditional compilation allows us to compile certain features into a program depending on what features of the program are enabled. This allows us to ignore or include functions from a final build, and even gives us the freedom to emit individual statements.

This is exactly what’s happened in our fetch request demo, with the console_error_panic_hook crate. This crate is a part of the default features…



Ross Bulat

Programmer and Author. @ Parity Technologies, JKRB Investments