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

Budowanie i uruchamianie modułów WASI

Skompiluj kod Rust dla WASI i uruchamiaj moduły WebAssembly w różnych środowiskach uruchomieniowych WASI

Budowanie i uruchamianie modułów WASI to bezpłatna lekcja WebAssembly (WASM) for High Performance Apps na CoddyKit. To lekcja 2 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej WebAssembly (WASM) for High Performance Apps, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs WebAssembly (WASM) for High Performance Apps zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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.

Często zadawane pytania

Czy lekcja „Budowanie i uruchamianie modułów WASI” jest bezpłatna?

Tak — pełny tekst „Budowanie i uruchamianie modułów WASI” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu WebAssembly (WASM) for High Performance Apps, przejdź na CoddyKit PRO. Kurs WebAssembly (WASM) for High Performance Apps zawiera 4 lekcji w sumie.

Co nauczysz się w „Budowanie i uruchamianie modułów WASI”?

Skompiluj kod Rust dla WASI i uruchamiaj moduły WebAssembly w różnych środowiskach uruchomieniowych WASI Ćwiczysz WebAssembly (WASM) for High Performance Apps z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć WebAssembly (WASM) for High Performance Apps?

Nie wymagamy żadnego doświadczenia. WebAssembly (WASM) for High Performance Apps w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 2 z 4.

Ile czasu zajmuje lekcja „Budowanie i uruchamianie modułów WASI”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji WebAssembly (WASM) for High Performance Apps?

Tak. Każda lekcja WebAssembly (WASM) for High Performance Apps zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Wprowadzenie do WASI i jego celów
  2. Budowanie i uruchamianie modułów WASI
  3. Możliwości WASI i przyszłość
  4. Praca z systemem plików WASI
← Powrót do WebAssembly (WASM) for High Performance Apps