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
Copy file name to clipboardExpand all lines: component-model/src/language-support/rust.md
+64-17Lines changed: 64 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,11 @@
1
1
# Components in Rust
2
2
3
-
Rust has first-class support for the component model via [the `cargo component` tool](https://github.com/bytecodealliance/cargo-component). It is a `cargo` subcommand for
4
-
creating WebAssembly components using Rust as the component's implementation language.
3
+
Rust has first-class support for the component model via [the `cargo component` tool](https://github.com/bytecodealliance/cargo-component).
5
4
6
-
## Installing `cargo component`
5
+
`cargo component` is is a `cargo` subcommand for creating WebAssembly components
6
+
using Rust as the component's implementation language.
> You can find more details about `cargo component` in its [crates.io page](https://crates.io/crates/cargo-component).
15
17
16
-
## Building a Component with `cargo component`
18
+
## 2. Scaffold a Component with `cargo component`
19
+
20
+
Create a Rust library that implements the `add` function in the [`adder`world][adder-wit].
17
21
18
-
Create a Rust library that implements the `add` function in the [`example`
19
-
world](https://github.com/bytecodealliance/component-docs/tree/main/component-model/examples/example-host/add.wit). First scaffold a project:
22
+
First scaffold a project:
20
23
21
24
```sh
22
25
$ cargo component new add --lib &&cd add
23
26
```
24
27
25
-
Note that `cargo component` generates the necessary bindings as a module called `bindings`.
28
+
Note that `cargo component` generates the necessary bindings as a module called `bindings`.
29
+
30
+
## 3. Add the WIT world the Component will implement
26
31
27
-
Next, update `wit/world.wit` to match `add.wit` and modify the component package reference to change the
28
-
package name to `example:component`. The `component` section of `Cargo.toml` should look like the following:
32
+
Next, update `wit/world.wit` to match `add.wit`:
33
+
34
+
```
35
+
package docs:adder@0.1.0;
36
+
37
+
interface add {
38
+
add: func(x: u32, y: u32) -> u32;
39
+
}
40
+
41
+
world adder {
42
+
export add;
43
+
}
44
+
```
45
+
46
+
The `component` section of `Cargo.toml` should look like the following:
29
47
30
48
```toml
31
49
[package.metadata.component]
32
-
package = "example:component"
50
+
package = "docs:adder"
33
51
```
34
52
35
-
`cargo component` will generate bindings for the world specified in a package's `Cargo.toml`. In particular, it will create a `Guest` trait that a component should implement. Since our `example` world has no interfaces, the trait lives directly under the bindings module. Implement the `Guest` trait in `add/src/lib.rs` such that it satisfies the `example` world, adding an `add` function:
53
+
## 4. Generate bindings for our component
54
+
55
+
After performing these changes, we can re-generate bindings with `cargo component bindings`:
56
+
57
+
```console
58
+
cargo component bindings
59
+
```
60
+
61
+
`cargo component bindings` will generate bindings for the world specified in a package's `Cargo.toml`. In particular,
62
+
`cargo component` will create a `Guest` trait that a component should implement.
63
+
64
+
## 5. Implement the generated `Guest` trait
65
+
66
+
Implement the `Guest` trait in `src/lib.rs`, using the scaffolded code. Your code should look something like the following:
0 commit comments