What news from AWS re:Invent last week will have the most impact on you?
Amazon Q, an AI chatbot for explaining how AWS works.
Super-fast S3 Express storage.
New Graviton 4 processor instances.
Emily Freeman leaving AWS.
I don't use AWS, so none of this will affect me.
Rust / Software Development / WebAssembly

Rust Is Surging Ahead in WebAssembly (for Now)

Rust continues to be one of the best programming languages for building WebAssembly applications and supporting infrastructure.
Oct 19th, 2023 8:02am by
Featued image for: 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.”

But before describing why Rust adoption is speeding ahead for now in the near and mid-term, let’s look at Rust’s downsides. First, Rust is a very challenging programming language to learn, especially when compared to the relative simplicity of Go, JavaScript, Python, and arguably even C++. However, the outward difficulty of Rust can be deceiving, especially when it comes to creating, developing, and deploying applications on WebAssembly. This is because it has already proven itself as an ideal language for WebAssembly.

Even on a basic programming level, such as a “Hello, World!” program, Rust’s elegance is evident. Once you master Rust, it is indeed challenging to get the compiler to accept your code. However, once achieved, it should, or more likely will, run seamlessly compared to loading other languages like Python or JavaScript into the compiler, which often leads to numerous bugs.

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.

Another reason is related to the infrastructure being created for the code component model, with Rust emerging as the main lingua franca for much of it, just as it has for WebAssembly. For developers, WebAssembly should and eventually will transparently accommodate numerous, if not the majority of, languages. In theory, it already supports languages like Python and JavaScript.

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

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.

“Rust has a high entry barrier, especially for developers used to dynamic programming languages. Fortunately, the WebAssembly ecosystem is growing, and languages like Python and JavaScript are getting better support,” De Miguel Meana said. “However, Rust will continue as a relevant language, although we expect that more developers will take advantage of WebAssembly without learning it. Proposals like the component model simplify how you develop components in multiple languages and combine them regardless of the source programming language — it is exciting.”

Group Created with Sketch.
TNS owner Insight Partners is an investor in: Pragma, The New Stack, Docker.
THE NEW STACK UPDATE A newsletter digest of the week’s most important stories & analyses.