Skip to content

Introduction

About this tutorial

If you’re reading this, chances are you want to learn Rust. Well, you’ve come to the right place, but let’s talk about this tutorial a little first.

I decided to write it because I wanted to consolidate my own knowledge of Rust and because I thought I could help people new to it learn it from my perspective. There are quite a few tutorials on Rust around the internet — and at least one of them is very good and very official — but most of them sound like they were written by the kind of person who alphabetizes their spice rack. They’re not bad! They’re just intimidating. I wanted something more like the LYAH-style tutorial that helped a generation of people stop running away from Haskell.

I failed to learn Rust approximately two times before finally getting it. The first time I bounced off because I thought the borrow checker was bullying me. The second time I bounced off because I was sure the borrow checker was bullying me. The third time I realized: the borrow checker was, in fact, bullying me, but only because I was wrong. Once you make peace with that, you start to feel a strange kind of affection for it. Like a stern librarian. Or a very large dog.

So who is this tutorial for? I’d say it’s for anyone who has programmed in some other language for at least a little bit and now wants to learn Rust. You don’t need any particular background — Python, JavaScript, Java, Go, C, C++, whatever got you here will do. If you know what a function is, what a variable is, and what a loop is, you’ll be fine.

So what’s Rust?

Rust is a systems programming language that obsesses about three things: performance, reliability, and helping you not put your foot through your monitor. That third one is what makes it interesting. Plenty of languages are fast. Plenty are correct. Rust is trying to be both at the same time, and tell you why your code is wrong in plain English before it ever runs.

It is statically typed, which means every value has a known type at compile time and the compiler will catch you if you try to use a string where it wanted a number. The good news is that Rust’s compiler is great at inferring types, so you mostly don’t have to type them out yourself. Watch this:

🦀 main.rs

Run that. Then try changing let answer = 42; to let answer = "forty-two";. Watch the compiler chew you out. Then put it back and call it a day. That’s basically what learning Rust feels like, all the way down.

Rust is also a compiled language. You write code, you run cargo build, and out the other end pops a binary you can ship. There’s no runtime, no garbage collector, no virtual machine — just your program, doing exactly what your program said to do, very fast.

It is memory safe without using a garbage collector. This is the trick. Most languages either let you manage memory yourself (and accept that you’ll regularly launch yourself into the sun) or they hire a GC to do it for you (and accept the occasional thirty-millisecond pause while it sweeps under the couch). Rust does neither. It uses a system called ownership to figure out, at compile time, exactly when each piece of memory is no longer needed, and frees it then. The result is a language that’s as fast as C, doesn’t crash like C, and complains at you like a slightly disappointed parent.

It has fearless concurrency, which sounds like marketing because it is, but it’s also kind of true. The same ownership rules that keep your memory safe also keep your threads from stomping on each other’s data. If your program compiles, it almost certainly doesn’t have a data race. Almost. (We’ll talk about unsafe later.)

It is expressive. You get match, you get iterators, you get closures, you get generics, you get traits (Rust’s answer to interfaces and typeclasses), you get pattern matching that destructures the heck out of any data structure you care to throw at it. If you’ve used Haskell, OCaml, or Scala, you’ll see old friends. If you haven’t, you’ll make new ones.

It is friendly. This is the part that surprises people. The Rust compiler is the kindest compiler I have ever used. When something goes wrong, it doesn’t just say “syntax error near line 17.” It says, more or less, “hey, you’re trying to use foo after you moved it on line 14 — did you mean to clone it, or do you want to borrow it instead?” And then it draws you a little ASCII picture of where the problem is. We’ll see plenty of that.

What you need to dive in

Two things, technically: a Rust toolchain, and a place to write code. But since you can do every exercise in this book without installing anything, let’s break it down by ambition.

Ambition level 1: just read and click

You already have everything you need. Every code sample on this site is a real, running playground. Hit ▶ Run. Edit the code. Click ”↗ Playground” to pop a snippet into a full editor in a new tab. You don’t even have to take off your slippers.

Ambition level 2: write Rust on your own machine

Install Rust the way the cool kids do, using rustup. On macOS or Linux:

Terminal window
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

On Windows, grab the installer from the same site. Either way, you’ll end up with:

  • rustc — the compiler itself.
  • cargo — the build tool / package manager / test runner / pretty much everything-else-runner. You’ll spend most of your life talking to Cargo.
  • rustfmt — formats your code automatically so you don’t have to argue about it.
  • clippy — a delightful little linter that catches your dumber moments and suggests fixes.

You can then make a new project with:

Terminal window
cargo new hello-rust
cd hello-rust
cargo run

…which prints Hello, world! and makes you feel powerful.

Ambition level 3: an editor that helps

Get VS Code (or your editor of choice) plus the rust-analyzer extension. rust-analyzer is the language server that gives you autocomplete, jump-to-definition, inline type hints, and red squiggles under your mistakes. It is, without exaggeration, one of the best language servers in any language. You can write Rust in any text editor on Earth, but rust-analyzer is the one that makes the whole experience feel like Rust with a friend.

That’s it. That’s the introduction. Next up: actually writing some Rust. You’ve already written some Rust, technically, in the playground above. But in the next chapter we’ll do it on purpose.