You can also browse this source code online and clone the wasmtime repository to run the example locally.

This example shows off how to instantiate a wasm module using WASI imports.

Wasm Source code

fn main() {
    println!("Hello, world!");


//! Example of instantiating of instantiating a wasm module which uses WASI
//! imports.

// You can execute this example with `cargo run --example wasi`

use anyhow::Result;
use wasmtime::*;
use wasmtime_wasi::sync::{Wasi, WasiCtxBuilder};

fn main() -> Result<()> {

    // Define the WASI functions globally on the `Config`.
    let mut config = Config::default();
    Wasi::add_to_config(&mut config);

    let store = Store::new(&Engine::new(&config)?);

    // Set the WASI context in the store; all instances in the store share this context.
    // `WasiCtxBuilder` provides a number of ways to configure what the target program
    // will have access to.

    let mut linker = Linker::new(&store);

    // Instantiate our module with the imports we've created, and run it.
    let module = Module::from_file(store.engine(), "target/wasm32-wasi/debug/wasi.wasm")?;
    linker.module("", &module)?;
    linker.get_default("")?.typed::<(), ()>()?.call(())?;