It has been revealed that welcome! This tutorial shows you how to write a small drawing app in Rust, in the spirit of Paint. We open a window, draw rectangles, circles and lines with the mouse, move them around, pick a color, and export the drawing as an image.
We start from an empty cargo new. By the end, you have about 800 lines of Rust spread over fifteen or so files, with a single dependency: minifb, which opens the window and gives us the keyboard and the mouse. Everything else we write ourselves: filling pixels, drawing a line, checking whether the mouse is on a circle, converting an HSV color to RGB. (Related: Stop treating agents as one-off projects and start runn...)
The tutorial is split into 12 chapters. Each one ends with a program that compiles and does something you can see, even if at first it's just an image file with a red square in it.
I'm a developer and I've been doing data science for several years. But in my free time, I've always been drawn to low-level things: bare metal, and how a computer works at the very bottom of the stack.
I wrote this tutorial with someone in mind who is starting out, or simply curious, and who wonders how a program ends up showing something on the screen. It covers the basics of rasterization, that is, how a rectangle or a circle turns into pixels, starting from nothing and in small pieces you can understand one at a time.
That's mostly what I'd like to show: by moving forward step by step, in a language close to the machine, you end up with something that works and that you wrote entirely yourself. It takes time, but there's nothing magic about it. If this tutorial takes away a bit of that mystery and makes a few beginners want to look further down, it will have done its job. (Related: How to train a newly introduced language model from scr...)
This tutorial is a way to learn Rust by building something. I assume you've read the first chapters of the Rust Book (variables, functions, if, loops) but haven't written a real program with it yet. The heavier concepts, like enums, traits, borrowing, modules or error handling, come along chapter by chapter, when we need them.
Industry observers note that you don't need to know anything about graphics. As for math, you need Pythagoras for circles and a projection to tell whether the mouse is on a line. I explain it when we get there.
Each chapter has boxes that explain why Rust does things the way it does. If you already know, skip them. Same for the exercises at the end of each chapter: they're there if you want practice, but nothing that follows depends on them.
It has been revealed that I'd still recommend typing the code rather than copy-pasting it. It's slower, but typing is how you make mistakes, and fixing them is how you end up understanding what the compiler is trying to tell you.
Recent findings suggest that let's be upfront: at the end, you won't have GIMP. There's no brush, no layers, no undo. And technically, our app isn't even really a Paint. Paint paints pixels that can't be moved once they're down, whereas we keep a list of shapes that can be selected and moved. We come back to this in chapter 11, and the Going further page has ideas for adding a brush, a paint bucket or undo.
After that, Going further suggests ideas to keep going on your own, from freehand drawing to a version that runs in the browser.
According to recent reports, all the code is in the pages, you don't need anything else. But if you prefer, the GitHub repo has one tag per chapter: git checkout ch05-mouse gives you the exact code at the end of chapter 5, and git diff ch04-window ch05-mouse demonstrates everything it adds. It's also handy to start again from a clean state if you get lost. The Following along with git page explains all of this.
In a recent development, if you find a mistake, an unclear explanation or a piece of code that doesn't compile, open an matter on GitHub. It really helps.
If you're ready, start here: chapter 0.
