I’m convinced WebAssembly is the next big thing, and it stretches beyond the Web. Here’s my experience learning the basics with the help of Rust.

“If you want to understand something, write about it”. So I decided to follow the famous advice and tell a short story about my foray into the WebAssembly land.

What is WebAssembly?

WebAssembly (a.k.a. Wasm) is an open standard that includes specifications for bytecode, its text representation and a secure host environment that would execute the code. The initial aim was to run C code on the web, but eventually, a range of compilers and runtimes was developed. So now we can run WebAssembly without a web browser or JavaScript.

It’s not the first attempt to create a cross-platform runtime. How is WebAssembly different from the previous “write once, run anywhere” technologies?

  • It’s light and simple: no heavy virtual machines, no platforms with complex APIs
  • It’s an open standard: nothing is proprietary, nothing is for sale
  • There’s no single “primary” language as it is the case with Java or dotNet
  • No specialisation: as WebAssembly isn’t a platform, it can be used for anything

Now that I wrote this it looks strange that it is the four “have nots” rather than “haves”. I think it’s an important thing about WebAssembly: it’s not Java. It’s so simple, open and universal that it can run anywhere.

I can’t count how many times I saw the phrase that WebAssembly isn’t designed to replace JavaScript, but rather to complement it. Well, let’s be honest: I did my time as a web developer and I’m not a fan of JS. When I’m looking at WebAssembly, what I actually think is “Can it replace JavaScript?”. Many people focus on performance, and it is important, but what if I just don’t want to write JavaScript anymore? When will its unnatural monopoly come to an end?

WebAssembly may be the answer to that question.

What is Rust?

I wanted to start from a relatively low level — no complex frameworks, no tooling. I like to know how things work before I use a high-level framework. At the same time, I wasn’t determined enough to write WebAssembly by hand. And didn’t want to write any C code either.

Fortunately, we have Rust programming language. Rust is characterized by two main things: it doesn’t let you mismanage the memory (by applying its novel ownership system), and it doesn’t use garbage collection — which means there’s almost no runtime. These traits make it an ideal language to be compiled to bytecode and run on a light virtual machine.

Such as WebAssembly.

Prerequisites

For our little experiment we’ll need to install a few bits and pieces:

  • Install Rust: https://www.rust-lang.org/tools/install
  • Install wasm-gc optimiser: cargo install wasm-gc
  • Wasm-pack bundler: cargo install wasm-pack
  • Miniserve, a simple web server: cargo install miniserve
  • Wasmer, the runtime that will let us run WebAssembly outside the browser: https://wasmer.io/




#webassembly #rust #web-development #javascript #developer

WebAssembly and Rust: There and Back Again
3.05 GEEK