Ferris Talk # 8: Wasm loves Rust – WebAssembly and Rust beyond the browser

When people talk about WebAssembly (Wasm) today, most people immediately think of web development. But Wasm can do much more than challenge JavaScript for client-side programming. Wasm has launched a small revolution on the server side, which has the potential to produce innovative solutions to fundamental problems in the field of distributed systems that have not yet been resolved or are poorly resolved.




In this column, two Rust experts, Rainer Stropek and Stefan Baumgartner would like to report in turn on regular updates and background information in the Rust area. Helps teams already using Rust stay current. Beginners get deeper insights into how Rust works through Ferris Talks.

Wasm is currently breaking the JavaScript monopoly on browsers and companies are bringing large chunks of existing C / C ++ code into browsers using this technology. Languages ​​like C # with the Blazor framework or Rust with Yew are trying to take the first steps towards conquering the web client. Many developers are excited to be able to bring their favorite language, knowledge and code snippets they already have into browsers without having to dig deep into the world of JavaScript. This gets a lot of attention for Wasm as a new W3C standard.

In this part of the Rust column, we take a look at the latest developments in WebAssembly and how they affect future software architectures beyond the web browser. Finally, despite the Wasm platform and the independence of the programming language, it has close ties to Rust. What Go was for the development of container technology, Rust is for Wasm.

Let’s start with the question of why Wasm is needed on the server at all. At the heart of the open standard is the idea of ​​defining a portable binary format (i.e. not operating system or architecture specific), with memory and runtime, which serves as the compilation target for each programming language. Wasm code can be efficiently translated into system-specific machine language before execution, and during execution it offers the advantages of the respective target platform, such as specific hardware functions. Wasm code works in a sandbox and administrators can control how it communicates with the outside world (file system, network).

Before we dig deeper, let’s see how easy it is to create a “Hello World” program using Rust and Wasm:

A prerequisite is to install the wasm32-wasi compilation target for Rust with rustup using the following command: rustup target add wasm32-wasi. Once this step is completed, Rust programs can now be compiled into Wasm modules: cargo build --target wasm32-wasi

A Wasm operation time is required to run outside the browser. Various operating time environments are available, such as Wasmer and Wasmtime. Wasmtime was used in this article, so a Wasm program can be started with the following command line:

wasmtime target/wasm32-wasi/debug/hello_wasm.wasm

If you go through the checklist, you will encounter an error when trying to read a file in the Rust program. The following code results in the error message “Error: failed to find a pre-opened file descriptor that could be opened” ./text.txt “”:

vuse std::fs;
use anyhow::Result;

fn main() -> Result<()> {
    let content = fs::read_to_string("./text.txt")?;
    println!("{}", content);

    Ok(())
}

Listimi: Wasm “Hello World”

Only if the wasmtime call explicitly allows access to a directory (wasmtime --dir=. target/wasm32-wasi/debug/hello_wasm.wasmvini re dirparameter), the program can read the contents of the referenced file./text.txt. Behind the controlled access to the operating system APIs is the WebAssembly System Interface (WASI).

However, Wasm operating environments like wasmtime can do more than just execute Wasm modules on the command line. They also provide libraries that can be used to insert Wasm modules into applications. It does not matter if both parties – the host and the integrated component – are written in Rust or not. Wasm programming languages ​​and similar technologies can be mixed. To begin inserting Wasm modules into a Rust application, see “Using WebAssembly by Rust” in the Wasm documentation.

On the home page

Leave a Comment