Learn Wasmtime - 9 Code Examples & CST Typing Practice Test
Wasmtime is a fast, secure, and production-grade WebAssembly runtime built by the Bytecode Alliance. It runs WebAssembly modules outside the browser-on servers, desktops, edge infrastructure, and embedded systems-using WASI for safe system interaction.
Learn WASMTIME with Real Code Examples
Updated Nov 25, 2025
Code Sample Descriptions
Simple Wasmtime Rust Program
# wasmtime/demo/main.rs
use wasmtime::*;
fn main() -> anyhow::Result<()> {
let engine = Engine::default();
let module = Module::from_file(&engine, "hello.wasm")?;
let mut store = Store::new(&engine, ());
let instance = Instance::new(&mut store, &module, &[])?;
let run = instance.get_typed_func::<(), (), _>(&mut store, "run")?;
run.call(&mut store, ())?;
Ok(())
}
A basic Rust program that loads and runs a WebAssembly module using Wasmtime.
Wasmtime Rust Function Call
# wasmtime/demo/add.rs
use wasmtime::*;
fn main() -> anyhow::Result<()> {
let engine = Engine::default();
let module = Module::from_file(&engine, "add.wasm")?;
let mut store = Store::new(&engine, ());
let instance = Instance::new(&mut store, &module, &[])?;
let add = instance.get_typed_func::<(i32, i32), i32, _>(&mut store, "add")?;
let result = add.call(&mut store, (2, 3))?;
println!("Result: {}", result);
Ok(())
}
Calls a function named 'add' from a WASM module.
Wasmtime Rust Memory Access
# wasmtime/demo/memory.rs
use wasmtime::*;
fn main() -> anyhow::Result<()> {
let engine = Engine::default();
let module = Module::from_file(&engine, "memory.wasm")?;
let mut store = Store::new(&engine, ());
let instance = Instance::new(&mut store, &module, &[])?;
let memory = instance.get_memory(&mut store, "mem").expect("memory not found");
memory.write(&mut store, 0, &[1,2,3,4])?;
let mut buffer = [0;4];
memory.read(&mut store, 0, &mut buffer)?;
println!("Memory: {:?}", buffer);
Ok(())
}
Reads and writes WebAssembly memory.
Wasmtime Rust Host Function
# wasmtime/demo/host.rs
use wasmtime::*;
fn main() -> anyhow::Result<()> {
let engine = Engine::default();
let mut store = Store::new(&engine, ());
let func = Func::wrap(&mut store, |x: i32| {
println!("Called with {}", x);
});
let module = Module::from_file(&engine, "host.wasm")?;
let instance = Instance::new(&mut store, &module, &[func.into()])?;
Ok(())
}
Defines a host function callable from WASM.
Wasmtime Rust Global Access
# wasmtime/demo/global.rs
use wasmtime::*;
fn main() -> anyhow::Result<()> {
let engine = Engine::default();
let module = Module::from_file(&engine, "global.wasm")?;
let mut store = Store::new(&engine, ());
let instance = Instance::new(&mut store, &module, &[])?;
let global = instance.get_global(&mut store, "counter").expect("global not found");
global.set(&mut store, Val::I32(42))?;
println!("Global value set.");
Ok(())
}
Reads and sets a global variable in WASM module.
Wasmtime Rust Table Access
# wasmtime/demo/table.rs
use wasmtime::*;
fn main() -> anyhow::Result<()> {
let engine = Engine::default();
let module = Module::from_file(&engine, "table.wasm")?;
let mut store = Store::new(&engine, ());
let instance = Instance::new(&mut store, &module, &[])?;
let table = instance.get_table(&mut store, "tbl").expect("table not found");
table.set(&mut store, 0, Val::FuncRef(None))?;
Ok(())
}
Accesses and updates a WebAssembly table.
Wasmtime Rust Async Function
# wasmtime/demo/async.rs
use wasmtime::*;
use futures::executor::block_on;
fn main() -> anyhow::Result<()> {
let engine = Engine::default();
let module = Module::from_file(&engine, "async.wasm")?;
let mut store = Store::new(&engine, ());
let instance = Instance::new(&mut store, &module, &[])?;
let func = instance.get_typed_func::<(), i32, _>(&mut store, "compute")?;
let result = block_on(func.call_async(&mut store, ()))?;
println!("Async result: {}", result);
Ok(())
}
Calls an async WASM function with Wasmtime.
Wasmtime Rust Error Handling
# wasmtime/demo/error.rs
use wasmtime::*;
fn main() -> anyhow::Result<()> {
let engine = Engine::default();
let module = Module::from_file(&engine, "error.wasm")?;
let mut store = Store::new(&engine, ());
let instance = Instance::new(&mut store, &module, &[])?;
let func = instance.get_typed_func::<(), (), _>(&mut store, "fail")?;
match func.call(&mut store, ()) {
Ok(_) => println!("Success"),
Err(e) => println!("Error: {}", e),
}
Ok(())
}
Handles runtime errors from WASM function calls.
Wasmtime Rust WASI Program
# wasmtime/demo/wasi.rs
use wasmtime::*;
use wasmtime_wasi::{WasiCtxBuilder, Wasi};
fn main() -> anyhow::Result<()> {
let engine = Engine::default();
let module = Module::from_file(&engine, "wasi_program.wasm")?;
let mut store = Store::new(&engine, ());
let wasi = Wasi::new(&mut store, WasiCtxBuilder::new().inherit_stdout().build());
let instance = Instance::new(&mut store, &module, &[])?;
Ok(())
}
Runs a WASI WebAssembly module using Wasmtime.
Frequently Asked Questions about Wasmtime
What is Wasmtime?
Wasmtime is a fast, secure, and production-grade WebAssembly runtime built by the Bytecode Alliance. It runs WebAssembly modules outside the browser-on servers, desktops, edge infrastructure, and embedded systems-using WASI for safe system interaction.
What are the primary use cases for Wasmtime?
Running Wasm modules in servers or command-line environments. Embedding sandboxed plugins inside Rust/Go/Python/Node applications. Serverless compute and microVM-like execution. Edge compute environments. Running polyglot Wasm applications via WASI
What are the strengths of Wasmtime?
Fast startup and near-native execution. High security through sandboxing. Excellent Rust integration. Lightweight runtime suitable for microservices. Backed by major industry players
What are the limitations of Wasmtime?
GUI and browser APIs unavailable (server-side only). Limited POSIX compatibility (WASI still evolving). No built-in threading for Wasm without Wasm-Threads. File/network access requires WASI preview support. Ecosystem smaller than native runtimes
How can I practice Wasmtime typing speed?
CodeSpeedTest offers 9+ real Wasmtime code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.