You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// NOTE: here, we would normally use more of the wasi:cli interface
108
-
// to grab arguments and other information from the execution environment.
109
-
eprintln!("CLI => {}", self.greet("CLI User"));
82
+
eprintln!("Hello World!");
110
83
Ok(())
111
84
}
112
85
}
@@ -127,34 +100,59 @@ the interface and function to run (`wasi:cli/run` is detected and used automatic
127
100
128
101
```console
129
102
$ wasmtime run target/wasm32-wasip2/runnable-example.wasm
130
-
CLI => hello CLI User!
103
+
Hello World!
131
104
```
132
105
133
106
## Creating a command component
134
107
135
-
A _command_ is a component with a specific export that allows it to be executed directly by `wasmtime` (or other `wasi:cli` hosts). In Rust terms, it's the equivalent of an application (`bin`) package with a `main` function, instead of a library crate (`lib`) package.
108
+
A _command_ is a component with a specific export that allows it to be executed directly by `wasmtime`
109
+
(or other `wasi:cli` hosts). In Rust terms, it's the equivalent of an application (`bin`) package with
110
+
a `main` function, instead of a library crate (`lib`) package.
111
+
112
+
### 1. Create a new Rust binary project
136
113
137
-
To create a command with cargo component, run:
114
+
To create a command with cargo, run:
138
115
139
116
```sh
140
-
cargo component new <name>
117
+
cargo new runnable-example
141
118
```
142
119
143
-
Unlike library components, this does _not_ have the `--lib` flag. You will see that the created project is different too:
120
+
Unlike library components, this does _not_ have the `--lib` flag (`--bin` is the default for `cargo new`).
144
121
145
-
- It doesn't contain a `.wit` file. `cargo component build` will automatically export the `wasi:cli/run` interface for Rust `bin` packages, and hook it up to `main`.
146
-
- Because there's no `.wit` file, `Cargo.toml` doesn't contain a `package.metadata.component.target` section.
147
-
- The Rust file is called `main.rs` instead of `lib.rs`, and contains a `main` function instead of an interface implementation.
122
+
The created Rust source file is called `main.rs` instead of `lib.rs`, and contains a `main` function.
148
123
149
124
You can write Rust in this project, just as you normally would, including importing your own or third-party crates.
150
125
151
126
> All the crates that make up your project are linked together at build time, and compiled to a _single_ Wasm component. In this case, all the linking is happening at the Rust level: no WITs or component composition is involved. Only if you import Wasm interfaces do WIT and composition come into play.
152
127
128
+
### 2. Write the relevant Rust
129
+
130
+
The following code can be inserted into `runnable-example/src/main.rs`:
131
+
132
+
```rust
133
+
pubfnmain() {
134
+
eprintln!("Hello World!");
135
+
}
136
+
```
137
+
138
+
### 3. Build the component
139
+
140
+
To build the component, use `cargo`:
141
+
142
+
```sh
143
+
cargo build --target=wasm32-wasip2
144
+
```
145
+
146
+
### 4. Run the component with `wasmtime`
147
+
153
148
To run your command component:
154
149
155
150
```sh
156
-
cargo component build
157
-
wasmtime run ./target/wasm32-wasip1/debug/<name>.wasm
151
+
wasmtime run ./target/wasm32-wasip2/debug/runnable-example.wasm
158
152
```
159
153
160
-
> **WARNING:** If your program prints to standard out or error, you may not see the printed output! Some versions of `wasmtime` have a bug where they don't flush output streams before exiting. To work around this, add a `std::thread::sleep()` with a 10 millisecond delay before exiting `main`.
154
+
> [!WARNING]
155
+
> If your program prints to standard out or error, you may not see the printed output!
156
+
>
157
+
> Some versions of `wasmtime` have a bug where they don't flush output streams before exiting. To work
158
+
> around this, add a `std::thread::sleep()` with a 10 millisecond delay before exiting `main`.
0 commit comments