Skip to content

An introduction to the Rust programming language for Node developers.

License

Notifications You must be signed in to change notification settings

Mercateo/rust-for-node-developers

Repository files navigation

Rust for Node developers

An introduction to the Rust programming language for Node developers.

💡 2nd edition. I initially wrote this tutorial in the summer of 2016. Rust 1.0 was roughly a year old back than. This tutorial stayed quite popular over time even though I haven't added new chapters. As years passed by the Rust (and Node) ecosystem evolved further and this tutorial wasn't up-to-date with all changes. With the recent release of "Rust 2018" (which I'll explain later in more depth) I took the opportunity to update this tutorial as well. Enjoy the read! 🎉

Hi there, I'm Donald. I'm a JavaScript developer who wants to learn Rust and as a part of this process I'll write here about my learnings. But what is Rust and why do I want to learn it? Rust is a systems programming language like C or C++, but with influences from functional programming languages and even scripting languages like JavaScript. It feels very modern - which is no surprise, because it is a relatively young language. It went 1.0 in 2015! That doesn't only mean it is fun to write, because it has less clutter to carry around, it is also fun to use, because it has a modern toolchain with a great package manager. Rust's most unique feature is probably the compile-time safety check: it catches errors like segfaults without introducing a garbage collector. Or to phrase it differently: maximum safety with maximum performance.

Probably even more important than its features is the ecosystem and the community behind the language. Rust really shines here - especially for people who love the web. The language was (and still is) heavily influenced by developers from Mozilla. They have written servo, a modern browser engine, in Rust and parts of Firefox are now running Rust code. Rust is also great for authoring WebAssembly code (short: Wasm), a binary format for the web, which is supported in Firefox, Edge, Chrome and Safari.

To summarize: Rust is a young modern language with a great tooling and ecosystem, good safety and performance promises and which can be used for a lot of different projects - from low level tasks to command line tools and even web projects.

Before we dive into our tutorial we want to look at least once into a real Rust file:

fn main() {
    println!("Hello World!");
}

The JavaScript equivalent could roughly look like this:

function main() {
  console.log('Hello World!');
}

Nothing too scary, right? The ! behind println could be a bit confusing, but don't mind it for now. Just think of it as a special function.

How do we move on from here? First I'll guide you how my current setup looks like to use Node and Rust. Many people seemed to like that I introduce some convenient tooling and IDE configurations before actually speaking about Rust itself. But you can skip this chapter, if you want. After the setup step I'll create several kitchen sink like examples - first with Node and then with Rust. I'll try to explain them as best as I can, but don't expect in-depth explanations in every case. Don't forget that I'm trying to learn Rust - just like you. Probably you need to explain me some things, too! And before I forget it: my Node examples will be written in TypeScript! Writing them in TypeScript will make it a little bit easier to compare some examples to Rust.

One word about the structure of this tutorial before we start. Every chapter has its own directory. If a chapter has sub-chapters they also have sub-directories. And if a (subd-)chapter contains code examples, you'll find a node and a rust directory which contain all the code of this (sub-)chapter. (One example: The chapter "Package Manager" can be found inside package-manager/. It has the sub-chapter "Publishing" and the corresponding code examples can be found in package-manager/publishing/node/ and package-manager/publishing/rust/.)

Table of contents

  1. Setup
  2. Hello World
  3. Package Manager
  4. Read files
  5. Write files
  6. HTTP requests
  7. Parse JSON

Thank you for reading this tutorial. ♥

I highly appreciate pull requests for grammar and spelling fixes as I'm not a native speaker. Thank you!