All the tutorial's code is in the chapter pages: you can follow it from start to finish without ever opening git. But the project's GitHub repository is organized so you can use it too, if you'd rather read the code directly, compare your code to mine, or pick up the tutorial at a specific chapter.
This page assumes you know the basics of git: cloning a repository, switching branches. If not, no worries, the website is all you need.
How the repository is organized#
The repository is on GitHub: theZackD/build-your-own-paint. It has two branches.
master holds the final code of the app, the one you get at the end of the tutorial.
chapters holds one commit per chapter, starting from an empty project. Its history is separate from master's: it tells the story of building the app in the order of the tutorial, step by step.
Each commit on the chapters branch has a tag, a label with the chapter's name. The ch05-mouse tag, for example, points to the exact code at the end of chapter 5. Every tag compiles and runs with cargo run.
The last tag, ch10-export, has exactly the same code as master. Chapter 11, the wrap-up, adds no code, so it has no tag.
| Tag | Chapter |
|---|---|
ch00-setup | 0. Setup |
ch01-vec2 | 1. 2D vectors |
ch02-ppm | 2. An image in memory |
ch03-shapes | 3. Drawing shapes |
ch04-window | 4. A window |
ch05-mouse | 5. The mouse |
ch06-shapes-enum | 6. Several shapes |
ch07-select-move | 7. Selecting and moving |
ch08-toolbar | 8. The toolbar |
ch09-colors | 9. Colors |
ch10-export | 10. Saving |
Getting the code#
git clone https://github.com/theZackD/build-your-own-paint.gitcd build-your-own-paintThe clone gets everything: both branches and all the tags. You can see the list of tags with:
git tagViewing the code of a chapter#
To get the code as it is at the end of a chapter, for example chapter 5:
git checkout ch05-mousecargo rungit then prints a long message that starts with:
Note: switching to 'ch05-mouse'.
You are in 'detached HEAD' state.No need to worry, that's normal. It means you're looking at a snapshot of the code at a precise moment, not a branch to work on. You can read the code, run it, switch to another tag. To go back to the final code:
git checkout masterIf you'd rather not install anything, you can also look at a chapter's code directly on GitHub, by putting the tag name in the address: github.com/theZackD/build-your-own-paint/tree/ch05-mouse.
Seeing what a chapter adds#
This is probably the most useful command. It shows every line added, changed or removed between two chapters:
git diff ch04-window ch05-mouseFor just the list of files touched, with the number of lines:
git diff --stat ch04-window ch05-mouse src/action.rs | 11 ++++++++
src/app.rs | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
src/canvas.rs | 2 ++
src/color.rs | 1 +
src/main.rs | 20 ++++++--------
src/shapes/mod.rs | 3 ++
src/shapes/rect.rs | 41 +++++++++++++++++++++++++++
src/vec2.rs | 8 ++++++
8 files changed, 156 insertions(+), 11 deletions(-)On GitHub, the same comparison is shown at github.com/theZackD/build-your-own-paint/compare/ch04-window...ch05-mouse.
Coding the tutorial inside the repository#
If you want to write the code yourself, chapter by chapter, while still being able to compare with mine, the most convenient way is to work directly in the cloned repository.
Create a branch of your own starting from a chapter, for example from the end of chapter 4 if you're about to do chapter 5:
git checkout -b my-attempt ch04-windowYou're now on your branch, my-attempt, and you can code chapter 5 by following the page on the website. When you're done, or if something doesn't work, compare your code with the code at the end of the chapter:
git diff ch05-mouseThis command shows everything that differs between your files and the tag. Everything doesn't have to be identical: different variable names or an extra comment are perfectly fine. What matters is that it works and that you understand why.
Remember to commit on your branch as you go, to keep track of your progress.
If you get lost#
It happens: an error you don't understand, a chapter that no longer compiles, the urge to start over. You can always start again from a clean version.
If you want to keep your changes aside for later:
git stashThen start again from the end of the previous chapter, on a new branch:
git checkout -b new-attempt ch04-windowgit stash puts your changes aside without losing them, and git stash pop will bring them back if you need them.
Reading on the website, coding elsewhere#
You don't have to code inside the repository. You can perfectly well create your own project with cargo new, as in chapter 0, and only use the repository to look at the reference code when in doubt. To print a file as it is at a given chapter, without switching branches:
git show ch05-mouse:src/app.rsOr more simply, open the file on GitHub by choosing the tag in the branch menu.
Reporting a problem#
If a tag doesn't compile for you, if the code on a page doesn't match the tag's, or if an explanation isn't clear, open an issue on GitHub. Mention the chapter, your system (Windows, macOS or Linux) and your Rust version (rustc --version).