Step by Step Guide on WASM with Rust

Reading Time: 3 minutes
This image has an empty alt attribute; its file name is rust-back4-1.jpg

WebAssembly is the future of Web Development. It is a new type of code that can be run in 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++ and Rust with a compilation target so that they can run on the web.

Hello, folks!  your wait is over, we have come up with a new topic WebAssembly with Rust. In this blog we will discuss the WebAssembly With Rust. Hope you will enjoy the blog.

WebAssembly (abbreviated Wasm) is a binary instruction format for a stack-based virtual machine. Wasm is designed as a portable target for compilation of high-level languages like C/C++/Rust, enabling deployment on the web for client and server applications. It is also designed to run alongside JavaScript, allowing both to work together.
WebAssembly is a portable binary instruction format. This is very similar to the original intent behind Java’s byte-code.

Image result for webassembly with rust

The operations encoded in a WebAssembly module are not tightly coupled to any one hardware architecture or operating system, and these operations are just codes that a parser knows how to interpret.
Next, there’s the phrase stack-based virtual machine. We’ll go over this in
detail soon, but the short explanation is that this stack machine simultaneously contributes to WebAssembly’s tremendous speed, power, and several of its limitations.

WebAssembly Is Not

  • WebAssembly is not meant to replace JavaScript. This is somewhat of a controversial opinion, as a large group of WebAssembly devotees online are convinced that it represents the death knell of JavaScript. While it might signal the beginning of a new era in which you write significantly less manual JavaScript, you still need JS to host WebAssembly 1.0 in the browser.
  • WebAssembly, on its own, isn’t a programming language. While there is both a binary and a text format, writing it by hand for anything beyond a few samples would take far too long and be too difficult to test and troubleshoot.

Why Rust?

Rust is a multi-paradigm system programming language that can provide better memory safety while maintaining high performance. It runs blazingly fast, prevents almost all crashes, and eliminates data races. It has an exceptional feature of memory management which can be achieved by the Ownership Concept.
Rust also provides powerful features such as zero-cost abstractions, safe memory management, fearless concurrency and many more. It also prevents segmentation faults and guarantees thread safety.

The number of libraries and tools available within the Rust community for WebAssembly is staggering. It is the enthusiasm, support, and rapid pace of advancement in the Rust WebAssembly community.

Let’s hands on with a example:

For hands-on the example of web assembly we will go through the WebAssembly Studio.

WebAssembly Studio is entirely online and lets you create new projects in C, Rust, or AssemblyScript, a tool for compiling Typescript to WebAssembly. It has gone through bursts of community activity and contribution since its creation.

Open up your browser and point it at webassembly.studio. You’ll come to a screen that presents the following options for creating a new project:

This image has an empty alt attribute; its file name is screenshot-from-2020-01-03-17-50-13.png

Choose “Empty Rust Project” and then click the Create button. This will create a new project within WebAssembly Studio, including a README.md file, a src directory, and the file you want to explore: main.rs . You’ll see the following code:

#[no_mangle]
pub extern "C" fn add_one(x: i32) -> i32 {
   x + 1
}

Using Rust’s no_mangle macro tells the compiler not to change the signature of the function during compilation.
Click the Build and Run button and you’ll see white square canvas in the bottom right-hand corner will display the number 42.

Congratulations, you’ve just written, compiled, and executed your first WebAssembly application.

If You don’t want to use WebAssembly Studio, Then go through this example Getting Hands-On with Wasm in Rust.

Note: I hope our blogs help you to enhance your learning. I’ll post more blogs on WebAssembly with Rust. Stay Tuned.
Happy learning!!!

References:

Mozilla WebAssembly.
WebAssembly.
Programming WebAssembly with Rust – Book.

This image has an empty alt attribute; its file name is footer-2.jpg

Written by 

Pankaj Chaudhary is a Software Consultant at Knoldus LLP. He has 1.5+ years of experience with good knowledge of Rust, Python, Java, and C. Now he is working as Rust developer and also works on machine learning and data analysis because he loves to play with data and extract some useful information from it. His hobbies are bike riding and explore new places.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading