Don't like this style? Click here to change it! blue.css
In this lab you will take a real open-source Rust project that generates procedural world maps, compile it to a WebAssembly component using WASI, and run it both natively and as a sandboxed WASM binary.
We are using rhysd/world-map-gen, a small Rust library and CLI that generates random maps with altitude and land types (sea, forest, mountain, town, etc.).
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source "$HOME/.cargo/env"
rustup target add wasm32-wasip2
curl https://wasmtime.dev/install.sh -sSf | bash
git clone https://github.com/rhysd/world-map-gen.git
cd world-map-gen
cargo build --release
cargo run --release -- --help
cargo run --release -- -s 42 -w 40 -h 20
Look around in the src/ directory. The interesting modules are gen, board, and draw.
cargo build --target wasm32-wasip2 --release
The output is at:
target/wasm32-wasip2/release/world-map-gen.wasm
Run it with Wasmtime:
wasmtime run target/wasm32-wasip2/release/world-map-gen.wasm -- -s 1337 -w 30 -h 15
The output should match the native run. You have just turned the same Rust code into a portable, sandboxed component.
If you want a smaller teaching binary, create a new project that depends on the library:
cargo new map-wasi
cd map-wasi
cargo add world-map-gen@0.1
Replace src/main.rs with:
use world_map_gen::gen;
fn main() {
let seed = std::env::args().nth(1).and_then(|s| s.parse().ok()).unwrap_or(42);
let board = gen::generate(40, 20, seed);
println!("{}", board);
}
Then build and run:
cargo build --target wasm32-wasip2 --release
wasmtime run target/wasm32-wasip2/release/map-wasi.wasm 999
You can also wrap the generator in an HTTP component (using Spin or Wasmtime’s serve command) so it answers HTTP requests. The original repository already contains a browser demo in the www/ directory if you want to see the same logic running in the browser via wasm-bindgen.
wasm32-wasip2..wasm with the wasm32-wasip2 target.wasmtime and confirm the output matches.