If you've made it this far, you've written a complete drawing app, starting from an empty folder. It opens a window, draws rectangles, circles and lines with the mouse, lets you select them, move them and delete them, has a color picker and saves the drawing to a file.
This chapter adds no code. We go over what we built, what we learned along the way, and two choices we made without saying much about them.
What we built#
The project is a little over 900 lines of Rust counting comments and blank lines, about 750 without. It's spread over fifteen files, and uses a single library, minifb, for the window, the keyboard and the mouse. Everything else, from the pixel to the color picker, we wrote ourselves.
| File | Role | Introduced in chapter |
|---|---|---|
src/main.rs | opens the window and runs the loop | 0, trimmed down in 5 |
src/vec2.rs | the Vec2 type and its operations | 1 |
src/image.rs | an RGB image saved as PPM | 2 |
src/color.rs | colors and their conversions, including HSV | 3, extended in 9 |
src/canvas.rs | the grid of pixels and the drawing functions | 3 |
src/config.rs | the size of the window | 4 |
src/app.rs | all of the app's state, and what it does at each frame | 5 |
src/action.rs | what we're currently doing with the mouse | 5, extended in 7 and 9 |
src/shapes/ | rectangles, circles, lines and enum Shape | 5, 6 and 7 |
src/ui/toolbar.rs | the buttons in the top-right corner | 8, extended in 9 |
src/ui/color_picker.rs | the color picker | 9 |
If you reread app.rs now, you should recognize every piece and know why it's there.
What we learned#
The drawing was an excuse. Each chapter introduced Rust concepts when we needed them:
- Chapter 1: structs, methods, traits for operators and printing,
Copy,OptionandResult. - Chapter 2:
Vec, indexing, borrowing with&selfand&mut self, files, and the?operator. - Chapter 3: bitwise operations, the difference between
usizeandi32, and what happens when a calculation overflows. - Chapter 4: crates,
Cargo.lock, the render loop,expect, and the difference between a key that's held and a key that was just pressed. - Chapter 5: a struct that groups the app's state, enums whose variants carry data,
Option::map,From, and borrowing different fields. - Chapter 6: enums that hold other types,
matchthat must cover every case, modules and closures. - Chapter 7: the borrow checker, and why we remember the selected shape by its index rather than by a reference.
- Chapter 8: arrays, looping over references, and why drawing order matters.
- Chapter 9: the HSV model,
clamp,matchreturning a tuple, and struct destructuring. - Chapter 10:
loopwith abreakthat returns a value,format!, and reusing code written much earlier.
Nobody remembers all of this after seeing it once. But you used each of these concepts for real, in a program that does something, and that's what helps most to make them stick.
An enum, or a trait?#
In chapter 6, to store different shapes in the same list, we chose an enum:
enum Shape {
Rect(Rect),
Circle(Circle),
Line(Line),
}Rust offers another solution, which you'll often come across in other projects: a trait. We could have written a Shape trait with the methods every shape must have, implemented it for each one, and stored the shapes in a Vec<Box<dyn Shape>>. To give you an idea, it would look like this. It's not meant to be typed in, just to look at:
trait Shape {
fn draw(&self, canvas: &mut Canvas);
fn contains(&self, p: Vec2) -> bool;
}
impl Shape for Rect {
fn draw(&self, canvas: &mut Canvas) {
// ...
}
fn contains(&self, p: Vec2) -> bool {
// ...
}
}
// same for Circle and Line
let shapes: Vec<Box<dyn Shape>> = vec![
Box::new(Rect::new(10.0, 10.0, 50.0, 30.0, BLACK)),
Box::new(Circle::from_points(center, edge, BLACK)),
];dyn Shape means "any type that implements the Shape trait". Since a rectangle and a circle don't take the same size in memory, you can't store them directly side by side in a Vec: we put each one in a Box, a small box stored somewhere else in memory, and the Vec only holds the boxes.
Both approaches work. The difference is in what's easy or not later on:
- With an
enum, the list of shapes is closed. Adding a new shape means changing theenumand everymatch, but the compiler gives you the exact list of places to complete. And when you want to add a new operation, liketranslatein chapter 7, you just write one morematch, in the same place as the others. - With a trait, the list is open. You can add a new kind of shape without touching existing code, which matters when other people need to be able to add their own shapes. On the other hand, every new operation has to be added to the trait and written in every type.
For us, who know all our shapes and mostly add operations, the enum was the simplest choice. Both are good Rust.
Why it's not really a Paint#
We said so on the home page: technically, our app isn't a Paint.
In Paint, when you draw, you paint pixels directly onto an image. Once they're down, they're part of the image: you can't pick up the rectangle you just drew to move it, it no longer exists as a rectangle, it's just colored pixels. That's what's called a raster program.
Our app does the opposite. It doesn't keep any pixel from one frame to the next: it keeps a list of shapes, each with its position, size and color, and redraws everything at each frame. That's why you can select a shape, move it or delete it: it still exists as a shape. That's what's called a vector program, like the drawing tools of a presentation program.
Each approach has its strengths. In vector, everything stays editable. In raster, some things become much more natural: a brush that leaves a freehand trail, an eraser that wipes out part of a shape, a paint bucket that fills an area. Those are precisely some of the ideas on the next page, and the first thing to decide when adding them will be to choose between the two worlds, or to mix them.
What now?#
The tutorial is over, but the app can go much further. The Going further page suggests ideas, from the most approachable to the most ambitious, each with the concepts to look up and a few hints, but no ready-made solution.
If you built the app all the way through, or changed it your own way, share it, I'd be glad to see it. And if something got you stuck in the tutorial, an unclear explanation or a piece of code that didn't compile, open an issue on GitHub: it will help the next people.