Rust
You need to install and WasmEdge in order to get started. You should also install the target to the Rust toolchain.
rustup target add wasm32-wasi
Hello world
The Hello world example is a standalone Rust application that can be executed by the . Its source code is available.
The full source code for the Rust file is as follows. It echoes the command line arguments passed to this program at runtime.
cargo build --target wasm32-wasi
$ wasmedge target/wasm32-wasi/debug/hello.wasm second state hello second state
The add example is a Rust library function that can be executed by the in the reactor mode.
The full source code for the Rust lib.rs file is as follows. It provides a simple add()
function.
fn main() {
#[no_mangle]
pub fn add(a: i32, b: i32) -> i32 {
return a + b;
}
We will use wasmedge
in reactor mode to run the program. We pass the function name and its input parameters as command line arguments.
Pass complex call parameters
Of course, in most cases, you will not call functions using CLI arguments. Instead, you will probably need to use a to call the function, pass call parameters, and receive return values. Below are some SDK examples for complex call parameters and return values.
To achieve native Rust performance for those applications, you could use the wasmedgec
command to AOT compile the wasm
program, and then run it with the wasmedge
command.
$ wasmedgec hello.wasm hello.wasm $ wasmedge hello.wasm second state hello second state
For the mode,
$ wasmedgec add.wasm add.wasm $ wasmedge --reactor add.wasm add 2 2 4
- Access OS services via WASI shows how the WebAssembly program can access the underlying OS services, such as file system and environment variables.
- shows how to create Tensorflow-based AI inference applications for WebAssembly using the WasmEdge TensorFlow Rust SDK.
- Simple networking socket shows how to create simple HTTP client and server applications using the WasmEdge networking socket Rust SDK.
- shows how to create a high-performance non-blocking networking applications with concurrent open connections using the WasmEdge networking socket Rust SDK.
- Server-side rendering shows how to build an interactive web app with Rust, and then render the HTML DOM UI on the server using WasmEdge. The Rust source code is compiled to WebAssembly to render the HTML DOM in the browser or on the server.
- shows how to create native command applications for WebAssembly using the Wasmedge command interface Rust SDK.