Please install the prerequisites first!
$ docker run --rm --runtime=io.containerd.wasmedge.v1 --platform=wasi/wasm secondstate/rust-example-function:latest
Hello WasmEdge!
Howdy WasmEdge!
Hola WasmEdge!
WasmEdge 你好!
The src/main.rs
source code shows
- The
greet()
function creates a vector ofGreeting
struct instances. It takes anLang
as input argument to look up the matchingGreeting
and returns its message. - The
greet()
function returns aResult
struct. The struct has two fields.- The first field is a
String
. It is the return value from the function. Inside the function, we returnOk(String)
to indicate success and wraps a string return value. - The second field is also a
String
. It is the error message when the function fails. We returnErr(String)
from inside the function to indicate failure and wraps an error message.
- The first field is a
- When we call the
greet()
function frommain()
, we use the?
notation to unwrap theResult
. Thegreet(Lang::English)?
provides the string value of the English greeting. If the English language is not supported in thegreet()
function, it will unwrap into a error and cause themain()
function to return the same error.
Compile the Rust source code project to a Wasm bytecode file.
$ cargo build --target wasm32-wasi --release
Run the Wasm bytecode file in WasmEdge CLI.
$ wasmedge ../target/wasm32-wasi/release/function.wasm
Hello WasmEdge!
Howdy WasmEdge!
Hola WasmEdge!
WasmEdge 你好!
The Dockerfile
follows the above steps to build and package a lightweight OCI-compliant container image for the Wasm app.
Now, we need to publish the container image to Docker Hub.
You just need to specify that the WasmEdge application image is for the wasi/wasm
platform.
$ docker buildx build --platform wasi/wasm -t secondstate/rust-example-function .
... ...
$ docker push secondstate/rust-example-function