Wasm Builders

Cover image for Let's do a "Hello world" program in Rust and executing in wasmtime runtime
Shraddha Vijay Inamdar
Shraddha Vijay Inamdar

Posted on

Let's do a "Hello world" program in Rust and executing in wasmtime runtime

Pre-requisites :

Please ensure that you have installed Rust and Cargo

 

Tool Setup

We need to install WASM Pack

cargo install wasm-pack
Enter fullscreen mode Exit fullscreen mode

Rust Code

First create a new binary crate using below command:

cargo new demo
Enter fullscreen mode Exit fullscreen mode

You can use any IDE of your choice and open up this project folder.

On Cargo.toml we are going to add the next:

[package]
name = "demo"
version = "0.1.0"
authors = ["Shraddha Inamdar <shraddha.inamdar95@gmail.com>"]
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]


[[bin]]
path = "src/main.rs"
name = "main"

Enter fullscreen mode Exit fullscreen mode

 

Writing Rust code

Writing a code for reading from file and writing to file using Rust.

  use std::env;
use std::fs;
use std::io::{Read, Write};

fn process(input_fname: &str, output_fname: &str) -> Result<(), String> {
    let mut input_file =
        fs::File::open(input_fname).map_err(|err| format!("Not Able To Open {}: {}", input_fname, err))?;
    let mut contents = Vec::new();
    input_file
        .read_to_end(&mut contents)
        .map_err(|err| format!("Not Able To Read: {}", err))?;

    let mut output_file = fs::File::create(output_fname)
        .map_err(|err| format!("Not Able To Open Output {}: {}", output_fname, err))?;
    output_file
        .write_all(&contents)
        .map_err(|err| format!("write error: {}", err))
}

fn main() {
    let args: Vec<String> = env::args().collect();
    let program = args[0].clone();

    if args.len() < 3 {
        eprintln!("usage: {} <from> <to>", program);
        return;
    }

    if let Err(err) = process(&args[1], &args[2]) {
        eprintln!("{}", err)
    }
}

Enter fullscreen mode Exit fullscreen mode

Compiling Rust Code

  1. Compile using cargo
cargo build --target=wasm32-wasi
Enter fullscreen mode Exit fullscreen mode
  1. The wasm file created in debug folder of wasi32
file target/wasm32-wasi/debug/main.wasm
Enter fullscreen mode Exit fullscreen mode
  1. Wasm runtime

wasmtime target/wasm32-wasi/debug/main.wasm
Enter fullscreen mode Exit fullscreen mode
  1. Created test.txt and output.txt with test.txt containing input
echo "Hi there, You made it! Cheers :)" > test.txt
Enter fullscreen mode Exit fullscreen mode
touch output.txt
Enter fullscreen mode Exit fullscreen mode
  1. Run using wasmtime

wasmtime target/wasm32-wasi/debug/main.wasm test.txt output.txt
Enter fullscreen mode Exit fullscreen mode
  1. Solving the above error using –dir

wasmtime --dir=. --dir=. target/wasm32-wasi/debug/main.wasm test.txt output.txt
Enter fullscreen mode Exit fullscreen mode
  1. Displaying the output
cat output.txt
Enter fullscreen mode Exit fullscreen mode

Discussion (0)