1# WASIp2 2 3You can also [browse this source code online][code] and clone the wasmtime 4repository to run the example locally. 5 6[code]: https://github.com/bytecodealliance/wasmtime/blob/main/examples/wasip2/main.rs 7 8This example shows how to use the [`wasmtime-wasi`] crate to define WASI 9functions within a [`Linker`] which can then be used to instantiate a 10WebAssembly component. 11 12[`wasmtime-wasi`]: https://crates.io/crates/wasmtime-wasi 13[`Linker`]: https://docs.rs/wasmtime/*/wasmtime/struct.Linker.html 14 15## WebAssembly Component Source Code 16 17For this WASI example, this Hello World program is compiled to a WebAssembly component using the WASIp2 API. 18 19`wasi.rs` 20```rust 21{{#include ../examples/wasm/wasi.rs}} 22``` 23 24> Building instructions: 25> 1. Have Rust installed 26> 2. Add WASIp2 target if you haven't already: `rustup target add wasm32-wasip2` 27> 3. `cargo build --target wasm32-wasip2` 28 29Building this program generates `target/wasm32-wasip2/debug/wasi.wasm`, used below. 30 31### Invoke the WASM component 32 33This example shows adding and configuring the WASI imports to invoke the above WASM component. 34 35`main.rs` 36```rust,ignore 37{{#include ../examples/wasip2/main.rs}} 38``` 39 40### Async example 41 42This [async example code][code2] shows how to use the [wasmtime-wasi][`wasmtime-wasi`] crate to 43execute the same WASIp2 component from the example above. This example requires the `wasmtime` crate `async` feature to be enabled. 44 45This does not require any change to the WASIp2 component, it's just the WASIp2 API host functions which are implemented to be async. See [wasmtime async support](https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.async_support). 46 47[code2]: https://github.com/bytecodealliance/wasmtime/blob/main/examples/wasip2-async/main.rs 48[`wasmtime-wasi`]: https://docs.rs/wasmtime-wasi/*/wasmtime_wasi/preview2/index.html 49 50```rust,ignore 51{{#include ../examples/wasip2-async/main.rs}} 52``` 53 54You can also [browse this source code online][code2] and clone the wasmtime 55repository to run the example locally. 56 57## Beyond Basics 58 59Please see these references: 60* The [book](https://component-model.bytecodealliance.org) for understanding the component model of WASIp2. 61* [Bindgen Examples](https://docs.rs/wasmtime/latest/wasmtime/component/bindgen_examples/index.html) for implementing WASIp2 hosts and guests.