0Pricing
WebAssembly (WASM) for High Performance Apps · Lesson

Building & Running WASI Modules

Compile Rust code to target WASI and execute your WebAssembly modules in various WASI runtimes.

Building & Running WASI Modules is a free WebAssembly (WASM) for High Performance Apps lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the WebAssembly (WASM) for High Performance Apps learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Building WASI Modules

Welcome back! In the previous lesson, we learned what WASI is and its goals. Now, let's get practical.

This lesson focuses on the hands-on process of compiling Rust code to target WASI and then executing your WebAssembly modules in various WASI runtimes.

Rust for WASI Development

Rust is a prime language for WASI development due to its performance, safety features, and strong support for WebAssembly. To get started, you'll need:

  • Rust Toolchain: Install it via rustup if you haven't already.
  • WASI Target: Add the specific WASI compilation target.
rustup install stable
rustup target add wasm32-wasi

Setting Up a WASI Project

We'll use Cargo, Rust's official package manager and build system, to create a new project. This sets up the basic directory structure.

cargo new my-wasi-app
cd my-wasi-app

Our First WASI Program

Let's write a simple Rust program. For WASI, basic I/O operations like printing to the console are handled through the WASI API. Open src/main.rs and add this code:

fn main() {
    println!("Hello from WASI!");
}

WASI's Role in I/O

When you use println! in a standard Rust program, it interacts directly with the operating system. For WASI, things are a bit different.

  • The WASM module is sandboxed.
  • println! calls are routed through the WASI API.
  • WASI provides a standardized interface for basic system functions like console output, file access, and environment variables.

Compiling to WASI

Now, let's compile our Rust code into a WebAssembly module specifically for the WASI target. This command tells Cargo to use the wasm32-wasi target we added earlier.

cargo build --target wasm32-wasi

The WASM Module Output

After successful compilation, you'll find your WASI WebAssembly module. It's a standalone binary file, ready for execution!

Look in the target/wasm32-wasi/debug/ directory for a file named my-wasi-app.wasm. This is your compiled WASI module.

Running with Wasmtime

To execute a WASI module, you need a WASI runtime. Wasmtime is a popular, high-performance runtime designed for this purpose.

  • Install Wasmtime: Follow instructions on wasmtime.dev for your OS.
  • Run your module: Execute the .wasm file using the wasmtime command.
# Install Wasmtime (example for Linux/macOS):
curl https://wasmtime.dev/install.sh -sSf | bash

# Run your compiled module:
wasmtime target/wasm32-wasi/debug/my-wasi-app.wasm

WASI File System Access

WASI allows controlled access to the host's file system. Let's modify our Rust program to read from a file. Remember, you'll need to explicitly grant Wasmtime permission to access files.

use std::fs;
use std::io::{self, Read};

fn main() -> io::Result<()> {
    let path = "input.txt";
    let mut file = fs::File::open(path)?;
    let mut contents = String::new();
    file.read_to_string(&mut contents)?;
    println!("File content: {}", contents);
    Ok(())
}

Running with File Access

After updating src/main.rs, compile again. Then, you'll need to create an input.txt file in your project root with some content. Finally, run it with Wasmtime, granting directory access:

# Create input.txt with content (e.g., "Hello WASI File!")
# Compile again:
cargo build --target wasm32-wasi

# Run, mapping current dir to WASI root:
wasmtime --mapdir /::. target/wasm32-wasi/debug/my-wasi-app.wasm

Quick Check

Let's test your understanding of building and running WASI modules.

Recap: Building & Running WASI

In this lesson, we explored the practical steps of building and running WebAssembly modules with WASI.

  • We set up our Rust environment for the wasm32-wasi target.
  • We wrote Rust code that leverages WASI for system interactions.
  • We compiled our code into a portable .wasm module.
  • We learned how to execute these modules using a WASI runtime like Wasmtime, including granting necessary permissions for host resource access.

Next, we'll delve deeper into WASI's capabilities and its implications for secure and portable applications.

Frequently asked questions

Is the “Building & Running WASI Modules” lesson free?

Yes — the full text of “Building & Running WASI Modules” is free to read here on the web, and the WebAssembly (WASM) for High Performance Apps course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the WebAssembly (WASM) for High Performance Apps course, upgrade to CoddyKit PRO.

What will I learn in “Building & Running WASI Modules”?

Compile Rust code to target WASI and execute your WebAssembly modules in various WASI runtimes. You practise WebAssembly (WASM) for High Performance Apps with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start WebAssembly (WASM) for High Performance Apps?

No prior experience is required. WebAssembly (WASM) for High Performance Apps on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Building & Running WASI Modules” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this WebAssembly (WASM) for High Performance Apps lesson?

Yes. Every WebAssembly (WASM) for High Performance Apps lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Introduction to WASI & Its Goals
  2. Building & Running WASI Modules
  3. WASI Capabilities & Future
  4. Working with the WASI Filesystem
← Back to WebAssembly (WASM) for High Performance Apps