Skip to content

Commit 3b6760f

Browse files
refactor(lang/rust): importing guide, simple build guide
1 parent 0ac2dca commit 3b6760f

File tree

2 files changed

+17
-11
lines changed
  • component-model/src/language-support
    • building-a-simple-component
    • importing-and-reusing-components

2 files changed

+17
-11
lines changed

component-model/src/language-support/building-a-simple-component/rust.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Components in Rust
1+
# Building a simple component (Rust)
22

33
[Rust][rust] has first-class support for WebAssembly core and WebAssembly components via the
44
available targets in the toolchain:

component-model/src/language-support/importing-and-reusing-components/rust.md

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ world calculator {
3131

3232
### Referencing the package to import
3333

34-
Because the `docs:adder` package is in a different project, we must first tell `cargo component` how to find it. To do this, add the following to the `Cargo.toml` file:
34+
Because the `docs:adder` package is in a different project, we must first tell `cargo` how to find it. To do this, we add a
35+
custom `wkg.toml` to our project:
3536

3637
```toml
37-
[package.metadata.component.target.dependencies]
38+
[overrides]
3839
"docs:adder" = { path = "../adder/wit" } # directory containing the WIT package
3940
```
4041

41-
> [!NOTE]
42-
> The path for `docs:adder` is relative to the `wit` _directory_, not to the `world.wit` file.
43-
>
44-
> A WIT package may be spread across multiple files in the same directory; `cargo component` will search them all.
42+
After adding this configuration file, when we run `wkg wit fetch`, `wkg` will assume that the package `docs:adder` can be found
43+
at the path that is given, and will pull it's contents into the local project under `wit/deps`.
44+
4545

4646
### Calling the import from Rust
4747

@@ -69,11 +69,16 @@ impl Guest for Component {
6969

7070
### Fulfilling the import
7171

72-
When you build this using `cargo component build`, the `add` interface remains imported. The calculator has taken a dependency on the `add` _interface_, but has not linked the `adder` implementation of that interface - this is not like referencing the `adder` crate. (Indeed, `calculator` could import the `add` interface even if there was no Rust project implementing the WIT file.) You can see this by running [`wasm-tools component wit`](https://github.com/bytecodealliance/wasm-tools/tree/main/crates/wit-component) to view the calculator's world:
72+
When you build this using `cargo build`, the `add` interface remains unsatisfied (i.e. imported).
73+
74+
The calculator has taken a dependency on the `add` _interface_, but has not linked the `adder` implementation of
75+
that interface - this is not like referencing the `adder` crate (Indeed, `calculator` could import the `add` interface even if there was no Rust project implementing the WIT file) .
76+
77+
You can see this by running [`wasm-tools component wit`](https://github.com/bytecodealliance/wasm-tools/tree/main/crates/wit-component) to view the calculator's world:
7378

7479
```
7580
# Do a release build to prune unused imports (e.g. WASI)
76-
$ cargo component build --release
81+
$ cargo build --target=wasm32-wasip2 --release
7782
7883
$ wasm-tools component wit ./target/wasm32-wasip1/release/calculator.wasm
7984
package root:component;
@@ -85,8 +90,9 @@ world root {
8590
}
8691
```
8792

88-
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
89-
only `calculate` is exported, you would need to [compose the `calculator.wasm` with some `adder.wasm` into a single, self-contained component](../../composing-and-distributing/composing.md).
93+
As the import is unfulfilled, the `calculator.wasm` component could not run by itself in its current form.
9094

95+
To fulfill the `add` import, so that only `calculate` is exported, you would
96+
need to [compose the `calculator.wasm` with some `adder.wasm` into a single, self-contained component](../../composing-and-distributing/composing.md).
9197

9298
[!NOTE]: #

0 commit comments

Comments
 (0)