Reader small image

You're reading from  Mastering Rust. - Second Edition

Product typeBook
Published inJan 2019
Reading LevelIntermediate
PublisherPackt
ISBN-139781789346572
Edition2nd Edition
Languages
Right arrow
Author (1)
Rahul Sharma
Rahul Sharma
author image
Rahul Sharma

Rahul Sharma is passionately curious about teaching programming. He has been writing software for the last two years. He got started with Rust with his work on Servo, a browser engine by Mozilla Research as part of his GSoC project. At present, he works at AtherEnergy, where he is building resilient cloud infrastructure for smart scooters. His interests include systems programming, distributed systems, compilers and type theory. He is also an occasional contributor to the Rust language and does mentoring of interns on the Servo project by Mozilla.
Read more about Rahul Sharma

Right arrow

Rust on the Web with WebAssembly

Rust extends far beyond its system programming domain and can be made to run on the web as well. In this chapter, we'll explore one such technology for doing this, called WebAssembly. We'll go into the details of what exactly WebAssembly is, and how Rust can be made to run alongside JavaScript using this technology.Being able to run on web browsers unlocks a domain that enables Rust to be used by a much larger audience, that is, the community of web developers, and enables them to leverage the performance of a systems language in their applications. Later in this chapter, we'll explore the tools and libraries that offer WebAssembly support, and build a live markdown editor that invokes an API implemented in Rust for rendering markdown documents to an HTML page.

In this chapter, we will cover the following topics

  • What is WebAssembly...

What is WebAssembly?

"Be curious. Read widely. Try new things. I think a lot of what people call intelligence boils down to curiosity." – Aaron Swartz

WebAssembly is a suite of technologies and specifications that allows native code to run on the web by compiling down to a low-level programming language called wasm. From a usability perspective, it's a set of technologies that allows programs written in other non-web programming languages to run on web browsers. From a technical standpoint, WebAssembly is a virtual machine specification with a binary, load-time-efficient Instruction Set Architecture (ISA). Okay, that's jargon overload. Let's simplify this definition a bit. As we know, a compiler is a complex program that turns code written...

Design goals of WebAssembly

WebAssembly's design is the result of a joint collaboration between major browser vendors. Together, they aim to shape its design with the following goals in mind:

  • Be as safe and universal as JavaScript: The web platform is an unsafe environment and having untrusted code run is detrimental to the safety of users on the web.
  • Run as quickly as native code: Because the language is quite compact, WebAssembly can be loaded faster than JavaScript code and can be interpreted five times faster than JavaScript.
  • Provide consistent, predictable performance: Being statically typed, and with very minimal analysis happening at runtime, WebAssembly is able to provide consistent performance on the web, whereas JavaScript falls short because of its dynamic nature.
  • Allow code reuse between web and native: A lot of existing code bases in C/C++, Rust, and other languages...

Getting started with WebAssembly

Though WebAssembly modules can be written by hand, it is not advisable to do so, as the code is hard to maintain and is not human-friendly. It is a pretty low-level language and, as a result, creating complex applications using raw wasm code can be challenging and time-consuming. Instead, it is often compiled to, or generated from, a variety of languages. Let's look at the available tools that we can use to explore the details of how WebAssembly programs are written and run.

Trying it out online

Before we discuss how WebAssembly is used as a compilation target across different language ecosystems, we can explore it online without requiring any kind of setup on our machine. Some tools that...

Rust and WebAssembly

The ecosystem around Rust and WebAssembly is hurtling at quite a fast pace and it'll take time before the community agrees on a set of tools for building practical applications. Fortunately, some tools and libraries are emerging that give us a picture of what to expect as developers when building applications for the web in Rust using WebAssembly.

In this section, we'll explore a crate from the community called wasm-bindgen. This crate is pretty much a work in progress, as the WebAssembly specification itself is a work in progress, but nonetheless, it is quite feature-rich to explore what is possible.

Wasm-bindgen

wasm-bindgen is a crate that was developed by the rust-wasm team on GitHub. It...

Summary

WebAssembly will have a huge impact on how web developers build their applications, allowing them to gain a lot of performance gains with little effort. It will allow for diversity in terms of application developers, thus allowing them to write web applications in their native language without worrying about learning other frameworks. WebAssembly is not meant to replace Javascript, but to serve as a high-performance language for running complex web applications on the web. The WebAssembly standard is constantly evolving and has a lot of exciting possibilities.

In this chapter, we learned about how Rust can compile down to wasm code and the available tooling that helps ship Rust code on the web. If you want to learn more about WebAssembly, head over to the excellent documentation at: https://developer.mozilla.org/en-US/docs/WebAssembly.

In the next chapter, we will learn...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering Rust. - Second Edition
Published in: Jan 2019Publisher: PacktISBN-13: 9781789346572
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Rahul Sharma

Rahul Sharma is passionately curious about teaching programming. He has been writing software for the last two years. He got started with Rust with his work on Servo, a browser engine by Mozilla Research as part of his GSoC project. At present, he works at AtherEnergy, where he is building resilient cloud infrastructure for smart scooters. His interests include systems programming, distributed systems, compilers and type theory. He is also an occasional contributor to the Rust language and does mentoring of interns on the Servo project by Mozilla.
Read more about Rahul Sharma