Before we can show a single pixel, we need a Rust project that compiles. This chapter is short: we install the tools, create the project, and take a look at what Cargo generated. If you already have Rust installed and know the basic commands like cargo new, you can jump to the end of the chapter, check your Rust version, and move on to chapter 1.
Installing Rust#
Rust is installed with rustup, a small tool that manages compiler versions. On macOS and Linux, paste this command into a terminal:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | shChoose the default installation when asked, then open a new terminal so the commands can be found.
On Windows, download rustup-init.exe from rustup.rs and run it. If you're missing the Visual Studio build tools, it will offer to install them: say yes, Rust needs them to produce executables.
Then check that everything is there:
rustc --version
cargo --versionYou need Rust 1.88 or newer. The project uses the 2024 edition of the language and a syntax (if let chained with &&) that only exists since that version. If you've had Rust installed for a while, update it:
rustup updateThe default installation also includes rustfmt, which formats code, and clippy, which points out clumsy code. We'll use them a bit further down.
An editor that understands Rust#
Any editor will do, but I strongly recommend using rust-analyzer. It shows errors as you type, displays the types of your variables and completes method names. In VS Code, it's the "rust-analyzer" extension. NeoVim, RustRover, Zed and most recent editors support it too.
With Rust, it really helps: the compiler is demanding, and seeing its remarks as you go is much nicer than getting them all at once when you compile.
Creating the project#
Go to the folder where you keep your projects and run:
cargo new build-your-own-paint
cd build-your-own-paintCargo creates this tree:
build-your-own-paint/
├── .gitignore
├── Cargo.toml
└── src/
└── main.rsIt also initializes a git repository in the folder, hence the .gitignore. You don't have to use it, but it's handy for keeping track of your progress, chapter after chapter.
Cargo.toml describes the project:
[package]
name = "build-your-own-paint"
version = "0.1.0"
edition = "2024"
[dependencies]The [package] section gives the program's name, its version and the Rust edition it uses. The [dependencies] section is empty for now. It will stay that way until chapter 4, where we add the library that opens a window.
And src/main.rs contains the program itself:
fn main() {
println!("Hello, world!");
}main is the function called when the program starts. println! prints a line in the terminal. The ! means it's a macro and not a function: it's turned into code at compile time, which lets it, for example, check that the text in curly braces matches the values you pass it. We'll come back to this; for now, just remember that a name followed by ! is a macro.
To make the project a bit more ours, change the message:
fn main() {
println!("Hello, Paint!");
}One last thing: in chapter 2, our program will start writing image files. We don't want them to end up in git, so add a line to .gitignore right away:
/target
*.ppmCompiling and running#
cargo runCargo compiles the project and then runs the program. You should see:
Compiling build-your-own-paint v0.1.0 (/path/to/build-your-own-paint)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.14s
Running `target/debug/build-your-own-paint`
Hello, Paint!Two things have appeared in the folder. target/ contains everything the build produces, including the executable target/debug/build-your-own-paint. You can delete it whenever you like, Cargo will recreate it. Cargo.lock records the exact version of each dependency so the project builds the same way everywhere. We don't have any yet, so it's almost empty.
Cargo's other commands#
We'll mostly use cargo run, but three other commands will come up often:
cargo check # checks that the code compiles, without producing an executable
cargo fmt # reformats the code following Rust's standard style
cargo clippy # points out code that compiles but could be written bettercargo check is faster than cargo run when you just want to know whether the compiler agrees with you. cargo fmt saves you from thinking about formatting: all the code in this tutorial is formatted with it. And cargo clippy is a good teacher. When it complains, it explains why and suggests a fix. Run it from time to time, you'll learn things.
How to read what follows#
A few conventions before we start.
Each code block names the file it belongs to above it. When a file is long, I only show the part that changes, and // ... stands for the code that stays the same. At least once per chapter, the full file is available for anyone who wants to check they haven't missed anything.
The Rust behavior boxes explain why the language does things a certain way. They help you understand, but you can skip them if you just want to move on. The Pitfall boxes point out mistakes almost everyone makes at least once.
Every now and then, in the middle of a chapter, a Your turn box asks you to write a small function or complete a piece of code, always with concepts we've already seen. The solution is right below, hidden: click to show it. It's exactly the code we use afterwards, so if you'd rather skip the exercise, take the solution and carry on.
Each chapter also ends with a few exercises. They're optional, and the rest of the tutorial never depends on them.
Finally, the code for each chapter is available on GitHub, with one tag per chapter. If you get lost, you can compare your code to mine or start again from a clean version. The Following along with git page explains how.
Exercises#
-
Make a mistake on purpose: write
printn!instead ofprintln!, or forget the!, then runcargo run. Read the whole error message. Rust's errors are long, but they often tell you exactly what to fix. Might as well get used to reading them right away. -
Run
cargo build --release, then find where the resulting executable is. Run it directly, without going through Cargo.
Solution to exercise 2
It's in target/release/build-your-own-paint (with .exe at the end on Windows). You run it from the project folder with ./target/release/build-your-own-paint.
Recap#
- Rust is installed and updated with
rustup. We need version 1.88 or newer. cargo newcreates a project with aCargo.tomland asrc/main.rs.cargo runcompiles and runs the program,cargo checkjust checks that it compiles.cargo fmtandcargo clippyhelp you write clean Rust.
In chapter 1, we write our first real type: a 2D vector, which we'll use to represent the positions and sizes of everything we draw.