Skip to content

Commit c53901b

Browse files
Merge pull request #222 from vados-cosmonic/fix(rust)=wit-link-and-prose-for-interfaces-with-example
fix(lang/rust): link to old WIT, prose & interface use example
2 parents ff815db + 534c3ea commit c53901b

File tree

1 file changed

+7
-39
lines changed
  • component-model/src/language-support

1 file changed

+7
-39
lines changed

component-model/src/language-support/rust.md

+7-39
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@ cargo install cargo-component
1717
1818
## 2. Scaffold a Component with `cargo component`
1919

20-
Create a Rust library that implements the `add` function in the [`adder`world][adder-wit].
20+
Create a Rust library that implements the `add` function in the [`adder` world][adder-wit].
2121

2222
First scaffold a project:
2323

2424
```sh
25-
$ cargo component new add --lib && cd add
25+
$ cargo component new adder --lib && cd adder
2626
```
2727

2828
Note that `cargo component` generates the necessary bindings as a module called `bindings`.
2929

3030
## 3. Add the WIT world the Component will implement
3131

32-
Next, update `wit/world.wit` to match `add.wit`:
32+
Next, update `wit/world.wit` to match the [`adder` world][adder-wit]:
3333

3434
```
3535
package docs:[email protected];
@@ -95,7 +95,7 @@ cargo component build --release
9595
You can use `wasm-tools component wit` to output the WIT package of the component:
9696

9797
```
98-
$ wasm-tools component wit target/wasm32-wasip1/release/add.wasm
98+
$ wasm-tools component wit target/wasm32-wasip1/release/adder.wasm
9999
package root:component;
100100
101101
world root {
@@ -118,43 +118,10 @@ Rust bindings, bring in WASI worlds, and execute the component.
118118

119119
```sh
120120
$ cd examples/example-host
121-
$ cargo run --release -- 1 2 ../add/target/wasm32-wasip1/release/add.wasm
121+
$ cargo run --release -- 1 2 ../add/target/wasm32-wasip1/release/adder.wasm
122122
1 + 2 = 3
123123
```
124124

125-
## Exporting an interface with `cargo component`
126-
127-
The [sample `add.wit` file](https://github.com/bytecodealliance/component-docs/tree/main/component-model/examples/example-host/add.wit) exports a function. However, you'll often prefer to export an interface, either to comply with an existing specification or to capture a set of functions and types that tend to go together. For example, to implement the following world:
128-
129-
```wit
130-
package docs:[email protected];
131-
132-
interface add {
133-
add: func(x: u32, y: u32) -> u32;
134-
}
135-
136-
world adder {
137-
export add;
138-
}
139-
```
140-
141-
you would write the following Rust code:
142-
143-
```rust
144-
mod bindings;
145-
146-
// Separating out the interface puts it in a sub-module
147-
use bindings::exports::docs::adder::add::Guest;
148-
149-
struct Component;
150-
151-
impl Guest for Component {
152-
fn add(x: u32, y: u32) -> u32 {
153-
x + y
154-
}
155-
}
156-
```
157-
158125
## Importing an interface with `cargo component`
159126

160127
The world file (`wit/world.wit`) generated for you by `cargo component new --lib` doesn't specify any imports.
@@ -234,7 +201,8 @@ world root {
234201
}
235202
```
236203

237-
As the import is unfulfilled, the `calculator.wasm` component could not run by itself in its current form. To fulfill the `add` import, so that only `calculate` is exported, you would need to [compose the `calculator.wasm` with some `exports-add.wasm` into a single, self-contained component](../creating-and-consuming/composing.md).
204+
As the import is unfulfilled, the `calculator.wasm` component could not run by itself in its current form. To fulfill the `add` import, so that
205+
only `calculate` is exported, you would need to [compose the `calculator.wasm` with some `adder.wasm` into a single, self-contained component](../creating-and-consuming/composing.md).
238206

239207
## Creating a command component with `cargo component`
240208

0 commit comments

Comments
 (0)