WebAssembly Modules in Rust: Syntax Deep Dive
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
Futuresand utilities from
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
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