Rust Is Surging Ahead in WebAssembly (for Now)
Even the most stalwart Rust haters are finding it difficult to conjure arguments about why not to learn and use Rust when building WebAssembly applications and supporting infrastructure. The reasons are many, including significantly greater language support both historically and now, a better fit for computations inside WebAssembly modules, exceptionally lightweight and speedy for LLM (large language model), and other superlatives.
However, things are moving quickly as WebAssembly’s adoption increases, and with it, the dream of “deploy once, deploy anywhere” comes closer for any language as a standardized component model becomes finalized. At this stage, the majority of libraries are already prepared for essentially copying and pasting code into a WebAssembly module with Rust. This makes it more numerous compared to other languages, along with the availability of APIs and documentation.
The open source community is moving fast to work out WebAssembly’s shortcomings, such as the number of production-ready libraries it can offer that will likely accommodate most, if not ideally all, programming-language code. Rust is definitely worth learning and using now and in the future. But if you are patient, the other languages will eventually catch up.
“Traditionally, Rust has been one of the languages with better support for WebAssembly. Most of the tooling, documentation, and examples are written in Rust,” Angel M De Miguel Meana, a staff engineer at VMware’s Office of the CTO, told The New Stack, who has given numerous talks on WebAssembly, including the demo described in more detail below from WasmCon 2023. “It has been natural for developers interested in WebAssembly to assume that it is the language to learn.”
Indeed, because of its shared origins, Rust has always had first-tier WebAssembly support. “Many of the leading WebAssembly runtimes and tooling are written in Rust, so learning it is nearly a must if you want to contribute to and fully leverage the WebAssembly ecosystem,” Daniel Lopez Ridruejo, founder and former CEO of Bitnami (now part of VMware), told The New Stack. “You get the performance and safety Rust is known for, with a seamless path to the portability enabled by WebAssembly.”
Rust in Action
During a demo of object detection with TensorFlow and the MediaPipe API for Rust at WasmCon 2023, Michael Yuan, co-founder of Second State, which offers Wasm for Cloud Native environments, and maintainer of WasmEdge, showed with De Miguel Meana how Rust, once compiled, is comparatively simple and adapted for lightweight machine learning.
“It is as simple as it gets. This code does object detection in Rust, running in WebAssembly (Wasm). People often claim that Python is easy, while Rust is complex,” Yuan said. “But take a look at this — the code is in fairly large fonts and fits on a single screen. It’s also easy to read.”
The code in the demo consists of a main function that takes two file names: the model path and the image path. It uses a builder pattern to set up the model, runs it, and then detects and draws boxes around objects in the image, Yuan described.
The WebAssembly community’s adoption of Rust has arguably changed the landscape while streamlining how other languages can be adapted for WebAssembly continues. In the long term, particularly in the field of AI, Rust seems poised to maintain its status as the language of choice. This is due in large part to its relatively lightweight nature, measured in bytes rather than megabytes, as seen in languages like Python.
For example, as demonstrated during Yuan’s demo, edge applications can run a WebAssembly module as an endpoint alongside a Rust application on a very small device. Thanks to Rust’s lightweight code, it can handle high-level computing applications like machine learning inference on such small-edge devices. Achieving this level of high-powered application on a very small device, even with WebAssembly, would arguably be unfeasible with languages like Python.
Providing the object finder program with an image of a cat and a dog, as Yuan showed in the demo, and then compiling it using Cargo Build into WebAssembly, it is then possible to run the code in Wasm Edge for a potential WebAssembly deployment. Yuan showed how the user can then use the Wasm Edge command line and provide the image, like “example.jpg,” and specify an output image. When you run the application, it processes the image, detects objects (in this case, two boxes), and draws those boxes on the original image, creating the output, he said.
“What’s great about this is that it has zero Python dependencies, eliminating the need to install Python or set up containers around it, which can be quite large,” Yuan said. “The entire Wasm application is only a few megabytes, and it’s highly portable across various CPU and GPU architectures, thanks to Wasm’s portability. Moreover, it’s easily manageable with container tools like Docker, Podman, or Kubernetes. In essence, Wasm serves as a language glue to integrate various AI and inference frameworks seamlessly.”
Rust Not Easy
— BC Gain (@bcamerongain) September 19, 2023
Despite its numerous advantages, it is highly unlikely that Rust will emerge as the one-size-fits-all lingua franca for WebAssembly. Its difficulty to learn alone will likely stall many interested developers from making the leap, while, again, support for other languages will almost inevitably catch up.