Building components with Rust
The Rust ecosystem has excellent support for building WebAssembly components.
Starting with the wasm32-unknown-unknown
target used in the past for bare WebAssembly modules, extending to the
wasm32-wasip2
(which reached Tier 2 support in 2024) which has the latest and
greatest of the component model, Rust has done much to keep up with and help grow the WebAssembly ecosystem.
The main wasmCloud/wasmCloud
repository contains Rust exmaples directly in-tree,
with examples of components and providers listed there.
Rust's standard toolchain is WebAssembly-ready
Standard Rust tooling can be used with Rust's WebAssembly support. Often the only thing needed to be able to build a Rust webassembly project
is an added target (e.g. wasm32-wasip1
, wasm32-wasip2
), or a properly formatted rust-toolchain.toml
file in the project folder.
To install the wasm32-wasip1
or wasm32-wasip2
targets, you can use rustup
:
rustup target add wasm32-wasip2
Once tools are installed, components can be built with cargo
:
cargo build --target=wasm32-wasip2
The wasmCloud Shell (wash
) is the easiest way to build wasmCloud projects
Get started
In this walkthrough, we will create an compoennt that responds to HTTP requests using Rust.
This walkthrough requires:
- cargo
- wasmCloud Shell (
wash
) CLI 0.36.1+ for building and deploying components
Step 1: Build a project with wash
The wasmCloud Shell (wash
) CLI helps developers build component-based applications that can be deployed with wasmCloud, bundling and re-using language-specific tooling
to build wasmCLoud projects.
We can use wash new
to scaffold out a new Rust project:
wash new component http-test --template-name hello-world-rust
This will create a http-test
folder which we can cd
into:
cd http-test
We can then immediately run wash build
to build our project as a WebAssembly component:
wash build
Similar to other Rust projects, WebAssembly binaries are placed in target
after being built.
To reduce inconsistencies across langauges wash
consistently places built artifacts in the build
folder of a given wasmCloud project.
The wash inspect
subcommand enables us to examine the new component's imports and exports:
wash inspect --wit build/http_hello_world_s.wasm
You should see output that looks like the following:
package root:component;
world root {
import wasi:io/poll@0.2.2;
import wasi:clocks/wall-clock@0.2.2;
import wasi:random/random@0.2.2;
import wasi:io/error@0.2.2;
import wasi:io/streams@0.2.2;
import wasi:cli/stdout@0.2.2;
import wasi:cli/stderr@0.2.2;
import wasi:cli/stdin@0.2.2;
import wasi:http/types@0.2.2;
import wasi:cli/environment@0.2.2;
import wasi:cli/exit@0.2.2;
import wasi:filesystem/types@0.2.2;
import wasi:filesystem/preopens@0.2.2;
export wasi:http/incoming-handler@0.2.2;
}
Once our component is built, we can start a local developer loop to use our app locally:
wash dev
While wash dev
won't return, it will have started a HTTP server that you can send requests to.
wash dev
is able to start up a HTTP server automatically because it discovers your application's
dependencies via WIT.
After a second or two, in another console/tab, we can curl
the application that was deployed locally:
$ curl localhost:8000
Hello from Rust!
Next steps
- Explore capabilities you can use in your wasmCloud application.
- Learn how to build a capability provider in Rust.