Building Golem Components in Rust
Building Golem components written in Rust as it is described on the Building Components page is a single call of the cargo component build
command:
$ cargo component build
Generating bindings for example (src/bindings.rs)
Compiling example v0.1.0 (/Users/vigoo/tmp/doc-temp/example)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.33s
Creating component target/wasm32-wasi/debug/example.wasm
The target/wasm32-wasi/debug/example.wasm
file is a WebAssembly component ready to be uploaded to Golem.
It is recommended to compile a release build of the component before deploying it to Golem as it is more optimized and smaller in size:
$ cargo component build --release
Generating bindings for example (src/bindings.rs)
Compiling example v0.1.0 (/Users/vigoo/tmp/doc-temp/example)
Finished `release` profile [optimized] target(s) in 0.33s
Creating component target/wasm32-wasi/release/example.wasm
Make sure the Creating component ... line is printed. Some previous versions of cargo-component
failed silently in some cases, skipping the last part of packaging the built WebAssembly module into a component.
It is possible to verify that the result .wasm
is a valid WebAssembly component by using the wasm-tools
CLI tool and running:
$ wasm-tools print target/wasm32-wasi/release/example.wasm --skeleton
The top-level node must be component
and not module
.
IDE support
Any IDE supporting Rust can be used, however for creating the result WASM file, the cargo component build
command must be used instead of the usual cargo build
command that the IDE might use under the hood.
When using rust-analyzer
, read the following section of the cargo-component
documentation about how to configure it properly: https://github.com/bytecodealliance/cargo-component#using-rust-analyzer (opens in a new tab)