Getting started into Rust and WebAssembly
Rust is a systems programming language. It runs blazingly fast. It guarantees thread safety and promises no dangling pointer.
Why should I learn one more language, what is so exciting about Rust ?
- It has better ergonomics.
- Easier and explicit syntax with some sweet implicit syntaxes.
- It includes best of both worlds (the high and low level languages).
- Easier to learn and much more easier to debug. (look at the below error message from Rust).
- It evolves fast for giving a better experience to the developers.
Coming back to our next hero in this story, WebAssembly
WebAssembly in simple language, is running your native code (C / C++ / Rust and others) in the browser. It is fast, secure and maintainable.
They are a set of binary instruction format that runs on any stack based machine.
There were several attempts made by browser vendors to run the native code in browser. The one thing that was widely popular wasasm.js
. But even it had many limitations.
WebAssembly is binary encoded instructions that are size- and load-time-efficient. They are secure and run on sandboxed environment.
Wait, isn’t Javascript fast. Yes it is, you can achieve ~native speed in Javascript. But, the work around and amount of effort that you have to spend is high. But with WebAssembly you can get this right out of the box.
Maintainable Performance is the key.
Let us get started with Rust and WebAssembly.
To start, you will need to install Rust-lang (and use nightly version rustup default nightly
).
Then add wasm32-unknown-unknown
in the tool chain. This will make the Rust compiler to compile Rust code into WebAssembly ( rustup target add wasm32-unknown-unknown --toolchain nightly
).
Check out here for more detailed steps.
A sample hello-world setup is explained in detail here.
There are certain tools that will help you to achieve even more with Rust and WebAssembly.
- wasm-bindgen — source
- wasm-snip — source
- wasm-pack — source
Have a look at our newsletter here.