The Rusty Web

Note: The contents of this book are now significantly outdated. Check out rustwasm for the latest and greatest on how to integrate Rust ans WebAssembly.

Welcome! This guide illustrates using the Rust programming language to target the web. The basic design pattern this project explores uses Rust to implement CPU bound portions of an app while using existing web technologies to handle user facing, I/O bound pieces.

If you want to see this design pattern in action visit the website or checkout the source code. Otherwise, let's get started. First some definitions (the details of the definitions are not as important as the high level concepts):

Rust

Rust is a general purpose programming language sponsored by Mozilla Research. It is designed to be a "safe, concurrent, practical language", supporting functional and imperative-procedural paradigms. -Wikipedia-

Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. -Rust Website-.

WebAssembly

WebAssembly or wasm is an experimental efficient low-level programming language for in-browser client-side scripting, which is currently in development... WebAssembly is a portable stack machine which is designed to be faster to parse than JavaScript, as well as faster to execute. -Wikipedia-

WebAssembly or wasm is a new portable, size- and load-time-efficient format suitable for compilation to the web. -WebAssembly Website-

asm.js

asm.js is an intermediate programming language designed to allow computer software written in languages such as C to be run as web applications while maintaining performance characteristics considerably better than standard JavaScript. -Wikipedia-

An extraordinarily optimizable, low-level subset of JavaScript -asm.js Website-

Emscripten

Emscripten is a source-to-source compiler that runs as a back end to the LLVM compiler and produces a subset of JavaScript known as asm.js [and WebAssembly]. -Wikipedia-

The Purpose of this Guide

This guide aims to answer two questions:

Q. How do these technologies fit together?
A. Rust is a general purpose programming language which can be compiled to various targets. Two of these targets happen to be asm.js and WebAssembly. As part of the compilation process Emscripten is used as a backend to produce the asm.js or WebAssembly code.

WebAssembly is a new language which aims to be the assembly language of the web. Its goal is to be a representation which all browsers understand and which programming languages can target. It is not JavaScript.

Asm.js is a strict subset of JavaScript. Because of this, browsers can optimize its execution. It is valid JavaScript which can be run by all browsers that support JavaScript.

Let's distill this down into a single explanation of how these pieces fit together. The Rust compiler, using Emscripten as a backend, compiles Rust code into asm.js or WebAssembly which can be run by a browser.

Q. What problems can these technologies help solve?
A. These technologies address problems of tooling and performance. JavaScript has long been the only privileged language of the web and will, in all likelihood, continue to be the only high level language with native support. WebAssembly aims to provide a native platform which any high level language can target opening the door for better tooling.

WebAssembly has the potential to provide performance benefits in three areas (for more details see this series of posts on WebAssembly):

  1. Decreasing website asset size. The WebAssembly format should be more compact than the equivalent JavaScript.
  2. Decreasing JavaScript "compile" times. Despite what JavaScript engine is used some time is spent decoding and optimizing JavaScript.
  3. Decreasing runtimes of equivalent JavaScript code.

I believe a natural design pattern emerges from these two questions. A WebAssembly library is used to handle the CPU intensive portions of a webapp. This library will be written in a performant language. Rust is positioned well to be one such language. The library will then be plugged in and used by existing web technologies. The remainder of this guide will focus on the mechanics of this design pattern.

The guide starts with a focus on asm.js because asm.js is an older, more stable target and does not require special browsers to run. Many of the concepts discussed in the asm.js version transfer to WebAssembly.

What This Guide Is Not

This guide will not teach you Rust in the general sense. There are numerous resources for that: the new book, the old book, Rust by Example. This guide also assumes basic knowledge of Cargo, Rust's package manager, and Rustup, Rust's toolchain manager.

This guide will not teach you to be a web developer. Basic knowledge of JavaScript, HTML and how the web works in general is assumed.

Disclaimer

These are all relatively young technologies. Many of the benefits offered by this design pattern are not yet fully realized, and the APIs for using these technologies may drastically change and improve.

This guide is equally immature. Many places could use further explanation and some sections are incomplete. My plan is for this guide to develop and grow alongside the technologies it discusses.

results matching ""

    No results matching ""