Please install the prerequisites first!
$ docker run --rm --runtime=io.containerd.wasmedge.v1 --platform=wasi/wasm secondstate/rust-example-move:latest
The src/main.rs
source code shows
- When the
hello
string variable is passed into thetake()
function, it is no longer available outside oftake()
. - You can create a
clone()
of thehello
string variable to pass totake()
. This way, the originalhello
is still available outside oftake()
. - You can also pass a reference of the
hello
string to theborrow()
function. Since theborrow()
function only borrowed a reference of this variable, the originalhello
is still available outside ofborrow()
.- The
&string
reference can be automatically cast to an immutable&str
. So, theborrow()
function can also take a&str
as argument.
- The
- If the
hello
string is mutable and the passed reference is also mutable, you can make changes to the string through its reference inside theborrow_mut()
function and thehello
string outside of the function will be changed too. That is the "side effect" of pass-by-reference.
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/move.wasm
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-move .
... ...
$ docker push secondstate/rust-example-move