Why Rust is good for Web Assembly and path to learning it...
Sahil Garg
Posted on November 10, 2020
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:
Support
WebAssembly 1.0 has shipped in 4 major browser engines: Chrome, Firefox, Safari, legacy Edge. Source: 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 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
Supporting my argument: Microsoft: Why we used programming language Rust over Go for WebAssembly on Kubernetes app
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:
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:
A simple application created for shutting down the windows 10 OS through the start menu without using the mouse.
Shutdown
Description:
This was created by me as I am sometimes very lazy in shutting down the PC and feel lazy in doing those clicks to first open the start menu then clicking the power icon on the side and then clicking shutdown button to shutdown the PC. I made this script to simplify this task and then just click the start key on the keyboard and then start typing 'shutdown' as typing shutdown is much easier than those boring clicks. Hence, when the shutdown.exe appears on the side startmenu, just press 'enter' and its done.
How to compile
Compiling is easier, you have to open the directory and then in the cmd type
$ cargo build --release
And it will compile it for you.
Contribution
If you want to contribute to it, just do it man. Open the issue and put in the discription that you want to work…
Mount your external drive using nautilus and automate it with this script.
Mount-drive-in-linux
I tried mounting my HDD automatically with fstab but it is not giving me appropriate permissions. I tried everything. But then I found that if I mount using nautilus in gnome I was getting appropriate permissions. But here the problem was that I wasn't able to find automatic mount. But after researching I found that by using gio mount command. So, that is what I wrote a script for in rust. Now putting it in startup scripts, it will mount my HDD automatically on startup.
First of all open the src/main.rs and change the drive name al line 8 to what you wish to mount.
To run the project you need to install rust version 1.45.0. For reference you can see Technologies. Then run:
$ git clone https://github.com/Sahil2004/Mount-drive-in-linux.git
$ cd Mount-drive-in-linux
$
This helps to clear out all the logs left behind due to errors when installing a npm package.
Delete-npm-error-logs
This helps to clear out all the logs left behind due to errors when installing a npm package. I made this because I usually fix the errors in a single go before switching off my pc and do not want to keep extra stuff on the storage.
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/. This will give you somewhat good practice of Rust.
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.