Preface
This book teaches Rust to someone who already programs. If you have written loops and functions in Lux, Python, JavaScript, or anything of that family, you have what you need. You will not be told what a variable is. You will be told what Rust does with one, which is a different and more interesting question.
Rust asks more of you than a scripting language does, and gives more back. Two things will be new. The first is that types are checked before the program runs rather than while it runs, so a class of mistake that a scripting language reports at three in the morning is reported by the compiler at your desk. The second is ownership: Rust tracks, at compile time, which part of your program is responsible for each piece of memory. That is the part nobody arrives already knowing, and Chapter 5 is where it gets its due. Everything before that chapter is written so you can reach it without surprises.
The pace is brisk on purpose. Programs come first and explanation follows, and the explanation stops when the point has been made. A feature that you can already guess the shape of gets a sentence. A feature that will bite you gets a page. If you want the complete description of some corner of the language, the official Rust Programming Language and the Rust Reference are free online and this book is not trying to replace them. What this book is trying to do is get you writing real Rust in a week rather than a month.
Read it with a keyboard. Type the programs in, run them, then break them and read what the compiler says. Rust’s error messages are the best part of the tooling, and learning to read them is learning the language. The exercises are short and most of them are variations on the program just presented; do the ones that look interesting and skip the rest without guilt.
Getting the compiler
Install Rust from https://rustup.rs. That gives you rustc, the compiler,
and cargo, which manages projects and dependencies and is how you will do
nearly everything. Check the install with cargo --version.
The code here is Rust 2021 edition. The newest thing it uses is let ... else,
so any compiler from 2023 onwards has everything. Where a program’s output
depends on your machine, the text says so.
About the examples
Every complete program in this book lives in the examples directory beside the
text, one crate per chapter. From examples, running
cargo run -p ch01 --bin fahr2
builds and runs the program named fahr2 from Chapter 1. The programs are the
ones printed here, not adaptations, so if something does not behave the way the
text claims, the text is wrong and you should trust the compiler.
The programs that deliberately fail to compile live in examples/errors, one
per mistake. Every compiler message quoted in this book came from running
rustc on the file of that name, and Appendix C collects them.
A note if you are coming from Lux
Lux already taught you most of Rust’s data model without naming it. Values are
immutable unless you say otherwise, there is no null, a function that might fail
hands back an Option or a Result, and match has to cover every case. All
four of those are Rust, and they are the parts of Rust that people coming from
Python find strangest. You are further along than you think. Appendix B maps the
two languages construct by construct; read it now if you want the shortcut, or
later when something looks familiar and you want to check.