OUTREACHY CONTRIBUTION SERIES:
WebAssembly with rust
Rust is the most suitable language for WebAssembly. This is because of the small runtime, excellent efficiency, and a memory-safe language.
Today, we're going to look into running a simple calculator rust program using wasmtime.
Installing rust in ubuntu
The first step is to setup rust in your local OS, I'm using ubuntu so here's the command for that:
curl https://sh.rustup.rs -sSf | sh
Install Wasmtime
curl https://wasmtime.dev/install.sh -sSf | bash
Install the WebAssembly Rust toolchain:
rustup target install wasm32-wasi
Rust Code
We need to create a rust application with the command:
cargo new example
cd example/src
For a simple calculator, add the following code to the main.rs file:
use std::io::{stdin, stdout, Write};
fn read(input: &mut String) {
stdout().flush()
.expect("failed to flush");
stdin().read_line(input)
.expect("failed to read");
}
fn main() {
println!("welcome to engineer man's calculator!");
println!("---------");
loop {
let mut num1 = String::new();
let mut num2 = String::new();
let mut operator = String::new();
print!("what is the first number?: ");
read(&mut num1);
print!("what is the second number?: ");
read(&mut num2);
print!("what operation would you like to do? [+-*/]: ");
read(&mut operator);
let num1: f32 = num1.trim().parse().unwrap();
let num2: f32 = num2.trim().parse().unwrap();
let operator: char = operator.trim().chars().next().unwrap();
let operators = String::from("+-*/");
if !operators.contains(operator) {
println!("unknown operator");
continue;
}
let result = match operator {
'+' => num1 + num2,
'-' => num1 - num2,
'*' => num1 * num2,
'/' => num1 / num2,
_ => panic!("error in operator")
};
println!("the result of {} {} {} = {}", num1, operator, num2, result);
}
}
Compiling Rust Code
Compile using cargo to verify the code:
cargo build
cargo run
Compile to wasm
rustc main.rs --target wasm32-wasi3.
Run the program:
wasmtime main.wasm
Yaaay! We finally executed our calculator in rust using WASM!
Top comments (0)