Wasm Builders

Cover image for Number guessing game using rust and Wasm
Silvana
Silvana

Posted on

Number guessing game using rust and Wasm

Let's talk a little about Rust. Rust is a compiled multi-paradigm programming language developed by Mozilla Research. It is designed to be "secure, concurrent, and convenient", but unlike other secure languages, Rust does not use a garbage collector. Best of all, it has native support for WebAssembly.

besides that Rust is one of the relatively new (born in 2015) and powerful programming languages which combines the power of C++ with the safety of Java and other interpreted languages.

Image description

Rust programming provides efficiency and reliability for a variety of activities, including web app development, distributed service architecture, and cross-platform programs with strong command-line tools. A rich-type system, as well as key components like closures and iterators, are among the language’s most notable characteristics. Rust may also be cross-compile with other languages to interact with existing code, but it’s well for low-resource situations.

The ability to manage low-level details is one of the most significant advantages of utilizing a systems programming language such as Rust. Because of the so many companies are using, like facebook, Microsoft, Mozilla, Dropbox, Figman and so many others.

So we can see the importance of such a powerful language, its use combined with WebAssembly is a powerful secure development tool.

For a demonstration of its use, let's start by installing the setup.
First download the language dependencies in the following link: [https://www.rust-lang.org/tools/install]

The you have to install Wastime:
[https://wasmtime.dev/]

And them Install the WebAssembly Rust toolchain by running this command:

rustup target install wasm32-wasi

Now the Rust code:

First create a new project using the command below:

cargo new demo

Using an IDE of your choice, write a code. I chose a guessing game

Image description

Compiling Rust Code

  1. Compile using cargo

cargo build
cargo run

Image description

  1. Compile to wasm

cargo build --target=wasm32-wasi

he wasm file created in release folder of wasi32

Image description

file target/wasm32-wasi/release/demo.wasm

Wasm runtime

wasmtime target/wasm32-wasi/release/demo.wasm

This is the result!

Image description

Hope you like it, if you have any comments please let me know!

Discussion (0)