Build your own Paint
Follow with gitGoing further
EN/FR
00

Chapter 0 / 11

Setup

We install Rust, create the project and check that everything compiles.

Duration15 min
Concepts
  • rustup
  • cargo
  • Cargo.toml
  • rust-analyzer

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:

Terminal
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Choose 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:

Terminal
rustc --version
cargo --version

You 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:

Terminal
rustup update

The 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:

Terminal
cargo new build-your-own-paint
cd build-your-own-paint

Cargo creates this tree:

build-your-own-paint/
├── .gitignore
├── Cargo.toml
└── src/
    └── main.rs

It 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:

Cargo.tomlTOML
[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:

src/main.rsRust
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:

src/main.rsRust
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:

.gitignore
/target
*.ppm

Compiling and running#

Terminal
cargo run

Cargo compiles the project and then runs the program. You should see:

Output
   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.

Tip

dev and unoptimized mean the program is compiled quickly, but without optimizations. That's what we want while developing. If the app ever feels slow, run it with cargo run --release before looking any further: the difference can be huge.

Cargo's other commands#

We'll mostly use cargo run, but three other commands will come up often:

Terminal
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 better

cargo 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#

  1. Make a mistake on purpose: write printn! instead of println!, or forget the !, then run cargo 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.

  2. 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 new creates a project with a Cargo.toml and a src/main.rs.
  • cargo run compiles and runs the program, cargo check just checks that it compiles.
  • cargo fmt and cargo clippy help you write clean Rust.
Following along with git?

This chapter matches the tag ch00-setup.

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.

On this page

  1. Installing Rust
  2. An editor that understands Rust
  3. Creating the project
  4. Compiling and running
  5. Cargo's other commands
  6. How to read what follows
  7. Exercises
  8. Recap
ContentsNext →Chapter 12D vectors

Build your own Paint

A tutorial by theZackD

The code on GitHubFound a mistake? Open an issueCode under the MIT license