Rust with WebAssembly: Simple explanation with a nice example

Reading Time: 2 minutes

Now a days everyone is talking about WebAssembly. In this blog, I will explain how you can get started with Rust and WebAssembly fast with a simple template. But before we start, lets discuss what is webassembly, why Rust should be preferred for it.

What is WebAssembly:

We can think of webassembly as a type of bytecode that can be generated from any language. That bytecode can be compiled and run by modern web browsers. It’s a low-level, portable, binary format for the web that aims to speed up web apps.
It works in two stages. First, a webassembly module is generated from the source code. Once the module is built, it can be run anywhere.

Why Rust should be preferred for WebAssembly:

WebAssembly, also known as Wasm, is supported by 40 high-level programming languages including C and C++, Python, Go, Rust, Java, and PHP.
People are looking to leverage WASM for web because of its advantages over javascript like Efficiency, Standardization, Security, and Language Independence. People want WASM bundle to actually perform better than JavaScript
This is why languages like Rust and C/C++ are usually prime candidates for WASM use, because they both are languages with performance and efficiency as a focus.
Over most other languages, Rust has the advantage of having a very slim runtime (many call it no runtime), no garbage collection and well-defined memory allocation behaviour. Since the wasm32 environment is very memory-constrained, that’s a big reason for using Rust. Over C/C++, Rust has the advantage of having a modern development environment and directly supporting the target in its core tools. 

Weather report using Rust and Webassembly:-

We have created a nice template using Rust and Webassembly. You can find source code here https://github.com/knoldus/rust-webassembly-weather-report. This project is about Weather Information System.

Clone the code and follow below steps:-

1) We are using OpenWeather API. Please add your api key in get_response() method.

async fn get_response(location: &str) -> JsonValue {
    let url1 = "http://api.openweathermap.org/data/2.5/weather?q=";
    let url2 = "&appid=<apikey>";

    let url = [url1, location, url2].concat();

    let resp = reqwest::get(&url).await.unwrap().text().await.unwrap();

    json::parse(&resp).unwrap()
}

2) Execute npm install and npm start

3) Go to browser and run http://localhost:8080

Thank you for reading!!!

If you want to read more content like this?  Subscribe Rust Times Newsletter and receive insights and latest updates, bi-weekly, straight into your inbox. Subscribe Rust Times Newsletter: https://bit.ly/2Vdlld7 .


Knoldus-blog-footer-image

Written by 

Ayush is the Sr. Lead Consultant @ Knoldus Software LLP. In his 10 years of experience he has become a developer with proven experience in architecting and developing web applications. Ayush has a Masters in Computer Application from U.P. Technical University, Ayush is a strong-willed and self-motivated professional who takes deep care in adhering to quality norms within projects. He is capable of managing challenging projects with remarkable deadline sensitivity without compromising code quality.

Leave a Reply