Rust

Rust is one of the “first-class citizen” programming languages in the WebAssembly ecosystem. All WasmEdge extensions to WebAssembly also come with Rust APIs for developers. In this chapter, we will show you how to compile your Rust applications to wasm bytecode and to run in the WasmEdge runtime.

Prerequisites

You need to install Rust and WasmEdge in order to get started. You should also install the wasm32-wasi 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 WasmEdge CLI. Its source code is available.

The full source code for the Rust main.rs file is as follows. It echoes the command line arguments passed to this program at runtime.

  1. use std::env;
  2. fn main() {
  3. println!("hello");
  4. for argument in env::args().skip(1) {
  5. println!("{}", argument);
  6. }
  7. }

Hello world: Build the WASM bytecode

cargo build --target wasm32-wasi

Hello world: Run the application from command line

We will use the wasmedge command to run the program.

$ wasmedge target/wasm32-wasi/debug/hello.wasm second state hello second state

A simple function

The add example is a Rust library function that can be executed by the WasmEdge CLI in the reactor mode.

The full source code for the Rust lib.rs file is as follows. It provides a simple add() function.

  1. #![allow(unused)]
  2. fn main() {
  3. #[no_mangle]
  4. pub fn add(a: i32, b: i32) -> i32 {
  5. return a + b;
  6. }
  7. }

A simple function: Build the WASM bytecode

cargo build --target wasm32-wasi

A simple function: Run the application from command line

We will use wasmedge in reactor mode to run the program. We pass the function name and its input parameters as command line arguments.

$ wasmedge --reactor target/wasm32-wasi/debug/add.wasm add 2 2 4

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 language SDK from WasmEdge to call the function, pass call parameters, and receive return values. Below are some SDK examples for complex call parameters and return values.

Improve performance

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 --reactor mode,

$ wasmedgec add.wasm add.wasm $ wasmedge --reactor add.wasm add 2 2 4

Further readings

  • Access OS services via WASI shows how the WebAssembly program can access the underlying OS services, such as file system and environment variables.
  • Tensorflow 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.
  • Non-blocking networking socket 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.
  • Command interface shows how to create native command applications for WebAssembly using the Wasmedge command interface Rust SDK.