Skip to main content

WebAssembly with Rust

Rust WASI

Environment setup

To get started, you must install the following:

Rust

Go to rust-lang.org and follow the instructions using rustup.

Wasmtime

You will find wasmtime at wasmtime.dev

Install the WebAssembly Rust toolchain:

 rustup target install wasm32-wasi

To get started, lets use a demo that caculates a Fibonacci sequence (i.e. every number after the first two is the sum of the two preceding ones).

Rust Code

First create a new project using the command below:

cargo new demo

You can use any IDE of your choice and open up this project folder, add this to the main.rs file:

use std::io;

fn fib (n: u32) -> u32 {
if n <= 0 {
return 0;
} else if n == 1 {
return 1;
} fib(n - 1) + fib(n - 2)
}

fn main() {
let mut nth = String::new();

println!("Enter input: ");

io::stdin()
.read_line(&mut nth)
.expect("Failed to read line");

let nth: u32 = nth.trim().parse().expect("Please type a number!");

println!("Fibonacci: {}", fib(nth));

}

Compiling Rust Code

  1. Compile using cargo
cargo build
cargo run

Rust Screenshot1

  1. Compile to wasm
cargo build --target=wasm32-wasi

Rust Screenshot2

  1. The wasm file created in release folder of wasi32
file target/wasm32-wasi/release/demo.wasm
  1. Wasm runtime

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

Rust Screenshot3