Why Rust is good for Web Assembly and path to learning it...
Tags: rust, webassembly, webdev, webpack
Published: 2020-11-10 09:58:41

# Introduction to Web Assembly
WebAssembly is a new type of code that can be run in modern web browsers — it is a low-level assembly-like language with a compact binary format that runs with near-native performance and provides languages such as C/C++, C#, and Rust with a compilation target so that they can run on the web. It is also designed to run alongside JavaScript, allowing both to work together.
Also, it can be understood in a 2.15-minute video from Fireship:
{% youtube cbB3QEwWMlA %}
### Support
WebAssembly 1.0 has shipped in 4 major browser engines: Chrome, Firefox, Safari, legacy Edge. *Source: [https://webassembly.org/](https://webassembly.org/)*

#### Web assembly = wasm

# Why Rust is best for Web Assembly
Many different languages can be compiled down to WebAssembly, including C# and Go, so why not use them instead of Rust? While the use of a programming language is always influenced by personal preference, there are many reasons why Rust is the best tool for the job. Because these languages have large runtimes that must be included in the WebAssembly binary, they're only really practical for greenfield projects (i.e., they're practical only as JavaScript replacements). This [Go wiki article](https://github.com/golang/go/wiki/WebAssembly) on Wasm says the smallest achievable binary size uncompressed is around 2MB; this mirrors what I've seen. For Rust, which ships with an extremely minimal runtime (basically just an allocator), the "hello, world" example compiles to 1.6KB on my machine without any post-compile size optimizations (which could bring it down further).

This is not to say that the future of Go or C# in the browser is bleak—I'm quite excited about what might come from those efforts. But the reality is that these technologies will probably always be best for greenfield projects.

C and C++ ship with very small runtimes, just like Rust, and thus can be practical for embedding inside existing apps and libraries. However, Rust makes it really easy to create WebAssembly binaries that have fairly idiomatic JavaScript interfaces using the tools we'll explore in the other articles in this series, while the process in C and C++ is much more manual. The tooling in Rust is absolutely fantastic, and I think it makes the entire experience much more enjoyable. Rust is also a much more memory-safe language meaning that a whole class of bugs that are common in C and C++ are impossible to have in safe Rust. If you're used to memory-safe languages like JavaScript, Java, and C#, (and even if you're not), you probably want to go with Rust. 
You can read more about the benefits of using Rust with Web Assembly at: [Why using WebAssembly and Rust together improves Node.js performance](https://developer.ibm.com/technologies/web-development/articles/why-webassembly-and-rust-together-improve-nodejs-performance/)
Supporting my argument: [Microsoft: Why we used programming language Rust over Go for WebAssembly on Kubernetes app](https://www.zdnet.com/article/microsoft-why-we-used-programming-language-rust-over-go-for-webassembly-on-kubernetes-app/#:~:text=WebAssembly%2C%20an%20open%20standard%20supported,compiled%20Wasm%20binaries%20or%20modules.)

# How to learn Rust?
Learning Rust isn't a simple task but following a good path and a good guide will make it easier. First of all, you need to know the syntax. For this, I recommend this Rust Crash Course by Traversy Media on youtube: {% youtube zF34dRivLOw %}
Then after this, the next thing to do is to make a real-world project, not a practice script. Let's get into some of the things that can be made using this. I made many scripts in Rust to practice it. Like: {% github Sahil2004/shutdown %} {% github Sahil2004/Mount-drive-in-linux %} {% github Sahil2004/delete-npm-error-logs %} Also as they are open-sourced you can see their code and learn and then make something like this that solves some kind of problem. You can even make a module for python project and then import it into python with rust-cpython; a good article will be: [https://developers.redhat.com/blog/2017/11/16/speed-python-using-rust/](https://developers.redhat.com/blog/2017/11/16/speed-python-using-rust/). This will give you somewhat good practice of Rust.

# Getting into Web Assembly
First, you will need to look at some background concepts: [https://rustwasm.github.io/docs/book/background-and-concepts.html](https://rustwasm.github.io/docs/book/background-and-concepts.html). Now the reference will be this book: [https://rustwasm.github.io/docs/book/](https://rustwasm.github.io/docs/book/). This is a great way of learning Web Assembly.

# Conclusion
Web Assembly and Rust together can do wonders. Learning this combo can make you a better Web Developer. Web Assembly with Rust is something which only a few know now but the future is this.