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<!-- langtabs-start -->
36
37```rust
38{{#include ../examples/wasip2/main.rs}}
39```
40
41<!-- langtabs-end -->
42
43### Async example
44
45This [async example code][code2] shows how to use the [wasmtime-wasi][`wasmtime-wasi`] crate to
46execute the same WASIp2 component from the example above. This example requires the `wasmtime` crate `async` feature to be enabled.
47
48This does not require any change to the WASIp2 component, it's just the WASIp2
49API host functions which are implemented to be async. See [wasmtime async
50support](https://docs.wasmtime.dev/api/wasmtime/#async).
51
52[code2]: https://github.com/bytecodealliance/wasmtime/blob/main/examples/wasip2-async/main.rs
53[`wasmtime-wasi`]: https://docs.rs/wasmtime-wasi/*/wasmtime_wasi/preview2/index.html
54
55<!-- langtabs-start -->
56
57```rust
58{{#include ../examples/wasip2-async/main.rs}}
59```
60
61<!-- langtabs-end -->
62
63You can also [browse this source code online][code2] and clone the wasmtime
64repository to run the example locally.
65
66## Beyond Basics
67
68Please see these references:
69* The [book](https://component-model.bytecodealliance.org) for understanding the component model of WASIp2.
70* [Bindgen Examples](https://docs.rs/wasmtime/latest/wasmtime/component/bindgen_examples/index.html) for implementing WASIp2 hosts and guests.
71