0Pricing
WebAssembly (WASM) for High Performance Apps · 课时

构建并运行 WASI 模块

将 Rust 代码编译为 WASI 目标,并在各种 WASI 运行时中执行 WebAssembly 模块

构建并运行 WASI 模块 是 CoddyKit 上的免费 WebAssembly (WASM) for High Performance Apps 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 WebAssembly (WASM) for High Performance Apps 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 WebAssembly (WASM) for High Performance Apps 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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.

常见问题解答

「构建并运行 WASI 模块」课时是免费的吗?

是的 — 「构建并运行 WASI 模块」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 WebAssembly (WASM) for High Performance Apps 课程的其余内容,请升级到 CoddyKit PRO。 WebAssembly (WASM) for High Performance Apps 课程共包含 4 节课。

「构建并运行 WASI 模块」这节课中我会学到什么?

将 Rust 代码编译为 WASI 目标,并在各种 WASI 运行时中执行 WebAssembly 模块 你通过在浏览器中直接运行的动手代码来练习 WebAssembly (WASM) for High Performance Apps,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 WebAssembly (WASM) for High Performance Apps 需要有经验吗?

无需任何先前经验。CoddyKit 上的 WebAssembly (WASM) for High Performance Apps 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「构建并运行 WASI 模块」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 WebAssembly (WASM) for High Performance Apps 课中编写并运行代码吗?

能。每节 WebAssembly (WASM) for High Performance Apps 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. WASI 简介与目标
  2. 构建并运行 WASI 模块
  3. WASI 的能力与未来
  4. 使用 WASI 文件系统
← 返回 WebAssembly (WASM) for High Performance Apps