Please install the prerequisites first!
The src/main.rs source code shows
- When the
hellostring variable is passed into thetake()function, it is no longer available outside oftake(). - You can create a
clone()of thehellostring variable to pass totake(). This way, the originalhellois still available outside oftake(). - You can also pass a reference of the
hellostring to theborrow()function. Since theborrow()function only borrowed a reference of this variable, the originalhellois still available outside ofborrow().- The
&stringreference can be automatically cast to an immutable&str. So, theborrow()function can also take a&stras argument.
- The
- If the
hellostring is mutable and the passed reference is also mutable, you can make changes to the string through its reference inside theborrow_mut()function and thehellostring 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 --provenance=false --platform wasi/wasm -t secondstate/rust-example-move .
... ...
$ docker push secondstate/rust-example-move
Then, with Docker Desktop and Wasm support enabled, you can run it.
$ docker run --rm --runtime=io.containerd.wasmedge.v1 --platform=wasi/wasm secondstate/rust-example-move:latest