3 min read

In a blog post, the state of Rust and WebAssembly for 2018 was discussed by the Rust-Wasm team. The Rust and WebAssembly domain working group worked to make a shared vision into a reality: “Compiling Rust to WebAssembly should be the best choice for fast, reliable code for the Web.

With the evolution of ideas, another core value was formed: “Rust and WebAssembly is here to augment your JavaScript, not replace it.

Goals were set for the joint ecosystem.

#1 JavaScript interoperation with zero-cost

By leveraging zero-cost abstractions Rust enables fast and expressive code. The Rust team wanted to apply this principle to the whole JS interop infrastructure. Developers can write their own boilerplate to pass DOM nodes to wasm generated by Rust but that shouldn’t be the case.

Hence they created wasm-bindgen as the foundation for JavaScript interoperation with zero cost. The communication between JavaScript and WebAssembly is facilitated by wasm-bindgen. This generates glue code which developers would have had to write themselves.

With the wasm-bindgen ecosystem helps developers to:

  • Exporting rich APIs from Rust-generated wasm libraries. This makes them callable from JavaScript.
  • Import JavaScript and Web APIs into the Rust-generated wasm.

#2 Rust-Generated Wasm as an NPM library

Good integration is about fitting Rust-generated WebAssembly into the JavaScript’s distribution mechanisms. A big part of that is NPM. The Rust team built a wasm-pack to creating and publishing NPM packages from Rust and WebAssembly code. Sharing Rust-generated wasm modules is now as simple as:

wasm-pack publish

#3 To get developers productive fast

The Rust team wrote a Rust and WebAssembly book to teach all the ins and outs of WebAssembly development with Rust. It features a tutorial to build an implementation of the Conway’s Game of Life and teaches you how to write tests, debugging, and diagnosing slow code paths.

Getting a Rust-WebAssembly project set up initially involves a boilerplate and configuration that new users may find difficult or experienced ones may find as a waste of time. Hence the Rust team has created a variety of project templates for different use cases:

  • wasm-pack-template to create NPM libraries with Rust and Wasm.
  • create-wasm-app to create Web applications built on top of Rust-generated wasm NPM libraries.
  • rust-webpack-template to create whole Web applications with Rust, WebAssembly, and the Webpack bundler.
  • rust-parcel-template to create whole Web applications with Rust, WebAssembly, and the Parcel bundler.

#4 Rust-Generated Wasm needs to be testable and debuggable

wasm can’t log any panics or errors because by default as it doesn’t have any “syscall” or I/O functionality. Imports have to be manually added for that, and then instantiate the module with appropriate functions. To remedy this, and to ensure that panics are always debuggable, the Rust team created the console_error_panic_hook crate. It redirects panic messages into the browser’s devtools console.

For more details on the state of the joint ecosytem in 2018, visit the Rust and WebAssembly Blog.

Read next

Mozilla shares plans to bring desktop applications, games to WebAssembly and make deeper inroads for the future web

Red Hat announces full support for Clang/LLVM, Go, and Rust

WebAssembly – Trick or Treat?

Subscribe to the weekly Packt Hub newsletter. We'll send you the results of our AI Now Survey, featuring data and insights from across the tech landscape.

* indicates required