About this book
Hi 👋 I’m Jesse, and this is a free online book for anyone who is curious about the Rust programming language. I’m self-taught, and this is geared toward folks like me who learn by doing. My philosophy is “move slow and make things.”
This is about the length of a weekend project. Over the next six chapters, we will learn about Rust while building a rudimentary Markdown compiler—a command-line interface tool that takes a Markdown file as input, converts it to HTML, and writes that HTML to another file.
$ echo "# Hello, world!" > some.md
$ tinymd some.md
$ cat some.html
If you’d like to see what we’ll be building, check out the repo on Github.
- Create a new Rust project on the command line without errors
- Compile and build a simple "Hello, World" Rust project without errors
- Create a function without errors
- Create an integer variable without errors
- Print an integer variable to the command line without errors
- Create a string variable without errors
- Return a string variable from a function without errors
- Concatenate two strings without errors
- Print a string to the command line without errors
- Describe how a compiler works in general
- Create a vector without errors
- Read and parse command-line arguments without errors
- Implement a match block without errors
- Pass an argument to a function without errors
- Open a file without errors
- Read a file line-by-line without errors
- Describe how a Markdown compiler works
- Write to a file without errors
- Build a release version of a project in Rust
How this book is written
Throughout this book you’ll find code snippets and command snippets.
Code snippets are blocks of code that you should be writing in your code editor:
Command snippets are commands you execute in your terminal:
$ echo "Hello, world!"
You may also find expandable pieces of information;
This book assumes that you
- have Rust installed,
- are familiar with the command line,
- have little to no experience with Rust, and
- have some experience with at least one other programming language.
If you don’t have Rust installed yet, head to rustup.rs and follow the instructions.
There are so many ways to start learning Rust that it can be overwhelming. It was for me. That’s why I wrote this book: to give people a real-life project to work on while learning the basics of Rust. Moving slow and building things is my favorite way to teach and learn, and I hope this book helps you see the joy of Rust just like I did.
When I first started learning Rust, I could only get a few weeks into it before my frustration would get the best of me and I would have to abandon it for a while. Eventually I would go back, have a few more “a-ha” moments, then get frustrated again and take a break. This cycle would continue for about four more times until finally I sat down with a pen and paper and wrote down exactly what it was that kept simultaneously bothering me about and attracting me to Rust.
What I arrived at was something that will likely not be a surprise to you: Rust was frustrating to learn because it required a different mental model when approaching programming problems. I couldn’t do what I did when going from C to C++, or C++ to PHP, or C++ to Java, or any of the other thirty or so languages I am familiar with, which was to just fit whatever syntactical differences the new language had into my existing framework for what a language should do and how it should feel.
Rust requires, for the most part, a fairly new way of thinking—especially if you come from languages that do a lot of things for you behind the scenes.
So I took a different approach to an introductory Rust experience. There’s only the basics here; variables and functions, reading and writing files, and some simple logical processing. We don’t cover any advanced features on purpose; Rust can be a productive and dare I say enjoyable experience depending on what you’re building and why.
In a survey of 6,000 Rust users, nearly 25% said the language was too hard or confusing to learn, with the two largest areas that all users said were very difficult being lifetimes and the ownership/borrowing system. We do cover ownership and borrowing a bit since it’s so germane to Rust, but we don’t cover lifetimes at all.