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 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 or wasm is a new portable, size- and load-time-efficient format suitable for compilation to the web. -WebAssembly Website-
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.
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?
WebAssembly has the potential to provide performance benefits in three areas (for more details see this series of posts on WebAssembly):
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.
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.