Wasm Builders 🧱

Cover image for Rust To WebAssembly for Non-Browser Case
Ajay Kumar
Ajay Kumar

Posted on

Rust To WebAssembly for Non-Browser Case

Prerequisites

  • Cargo

Cargo is Rust's build system and package manager, used to manage Rust projects such as building Rust code, downloading and building dependencies.
Installation

  sudo apt install cargo
  or
  curl https://sh.rustup.rs -sSf | sh
Enter fullscreen mode Exit fullscreen mode
  • Cargo-wasi

The cargo-wasi is a subcommand for Cargo which provides a convenient set of defaults for building and running Rust code on the wasm32-wasi target.

  cargo wasi build — build our code in debug mode for the wasi target.
Enter fullscreen mode Exit fullscreen mode

Installation

  cargo install cargo-wasi
Enter fullscreen mode Exit fullscreen mode
  • Wasmtime

Wasmtime is a runtime for WebAssembly, required because cargo-wasi used it as a default runtime.

Installation

  curl https://wasmtime.dev/install.sh -sSf | bash
Enter fullscreen mode Exit fullscreen mode

Command to create and run a Simple project Hello

  • Create a project name hello
 cargo new hello
Enter fullscreen mode Exit fullscreen mode

It will create a folder containing a subfolder src and a toml file having project information such as project name, version, dependencies, etc. while src folder have a rust file main.rs having a function named main with a greeting message Hello, world! from where execution get started.

  • Move inside hello
   cd hello
Enter fullscreen mode Exit fullscreen mode
  • Compile and Run Project hello
  cargo wasi run
Enter fullscreen mode Exit fullscreen mode

This will compile and run the hello project inside of wasm time automatically.
We can also run it using wasmtime like such:

  wasmtime target/wasm32-wasi/debug/hello.wasm
Enter fullscreen mode Exit fullscreen mode

Next we will see example for Rust Module
Previous tutorial

Top comments (0)