Chapter 1 · Rust and the rules

1. Set up

Pip, happy

Pip: Hi! I’m Pip, and I’m your teacher for this series. I’m a star, so puzzles that light up are kind of my thing.

Over this series we build a puzzle game called Plus Out. We write it in Rust, draw it with the Bevy game engine, and finish with a version that runs in your browser. This is where we’re heading:

The finished Plus Out: a 5 by 5 board of numbered tiles, with the taps left above it and the Undo, Hint and Restart buttons below it

This first chapter gets your tools ready and writes the first lines of the game’s rules.

You will end with a Rust project on your machine whose rules crate passes its first two tests.

How Plus Out plays

Plus Out is played on a board of 5 by 5 cells. Each cell holds a number from 0 to 3. A 0 is an empty cell, and the game draws it dark.

When you tap a cell, that cell and its four neighbours each go up by 1. The neighbours are the cells directly above, below, left and right of it. Together the five cells make a plus sign, which is where the game gets its name. Numbers wrap around, so a 3 that goes up by 1 becomes 0.

One tap on the centre of a board. Before it, the centre holds 3, the cells above and below hold 2, and the cells left and right hold 1. After it, the centre is 0, the cells above and below are 3, and the cells left and right are 2.

A cell on the edge has fewer neighbours. Tap a corner and only three cells change, because the board doesn’t wrap from one side to the other.

You win a board when every cell is 0. Each board also gives you only so many taps. Run out before the board is clear and you’ve failed that board, so you try it again.

Pip, curious

Pip: A quick puzzle before we write any code. A board holds five 3s in a plus shape in the middle, and every other cell is 0. How many taps clear it, and where?

Show the answer

One tap, right in the centre. All five 3s go up by 1, wrap around to 0, and the board is clear. Real boards are harder, because the pluses overlap and every tap changes cells you’ve already worked on.

Rules first, Bevy later

For the first four chapters we won’t touch Bevy at all. We write the rules of the game in plain Rust. The board comes in chapter 2, the puzzles in chapter 3 and a playing session in chapter 4. From chapter 5 on, Bevy draws those rules on screen and turns your clicks into taps.

Why not start with Bevy?

  • The rules can be tested without a window. A test that taps a board and checks the result runs in a blink. A test that has to open a window doesn’t.
  • Bevy changes faster than the rules do. A new Bevy release can rename things a game uses. The rules of Plus Out never change. With the two kept apart, a Bevy upgrade never touches the rules.
  • You meet one new thing at a time. In these chapters only Rust is new. When Bevy arrives with ideas of its own, the Rust underneath already feels familiar.

Pip, wink

Pip: I know, the window is the fun part. It comes in chapter 5, and by then you’ll read Rust well enough to enjoy it.

Install Rust

Rust is installed with a tool called rustup. It installs the compiler, rustc, and Rust’s build tool, cargo, and it keeps both up to date. You’ll talk to cargo all the time. It builds your code, runs your tests and fetches the libraries you use.

Go to rustup.rs and follow the steps for your system. On macOS and Linux that’s one command in a terminal. On Windows you download and run rustup-init.exe.

When it’s done, open a new terminal and check that both tools answer:

rustc --version
cargo --version

Each prints its version number. Don’t worry if yours is newer than 1.100.1. In a minute we tell the project exactly which version to use.

Do I need a special editor?

No, any editor works. If yours supports rust-analyzer, turn it on. It underlines mistakes while you type and shows the type of any value you hover over. VS Code, Zed and RustRover all support it.

Make the project folder

Make a folder for the game and step into it:

mkdir plus_out
cd plus_out
git init

git init is optional, but I recommend it. If you commit at the end of each chapter, you always have a working version to go back to.

Next, pin the Rust version. Create a file called rust-toolchain.toml in the plus_out folder:

[toolchain]
channel = "1.100.1"
components = ["rustfmt", "clippy"]

rustup reads this file every time you run cargo inside this folder. If Rust 1.100.1 isn’t installed yet, rustup downloads it first, together with rustfmt, which formats code, and clippy, which points out mistakes. Ask cargo for its version again to see the pin at work:

cargo --version
cargo 1.100.1 (3acc79c0c 2026-11-05)

Pip, calm

Pip: I ran every command in this series with exactly this version. When yours matches, what you see on your screen matches what I show you.

A workspace with one crate

Rust code is organised in crates. A crate is one library or one program, and it’s the unit cargo builds. Plus Out will have two crates:

  • plus_out_core holds the rules. It’s a library, so other code uses it, and it has no program of its own to run.
  • plus_out is the game itself, built with Bevy. It arrives in chapter 5.

A workspace groups crates that belong together. They share one target folder, where cargo puts everything it builds, and one Cargo.lock file, which records the exact version of every library they use.

Create a file called Cargo.toml in the plus_out folder:

[workspace]
resolver = "3"
members = []

members lists the crates in the workspace, and it’s empty for now. resolver = "3" tells cargo to pick library versions by its newest rules. Leave that line out and cargo warns you about it on every build.

Now let cargo make the rules crate:

cargo new core --lib --name plus_out_core

This makes a folder called core with a library crate inside, named plus_out_core. cargo reports what it did, with your own path in place of /path/to:

    Creating library `plus_out_core` package
      Adding `core` as member of workspace at `/path/to/plus_out`

It noticed the workspace and added the new crate to it. Open Cargo.toml and you’ll find members = ["core"].

Your folder now looks like this:

plus_out/
├── Cargo.toml
├── rust-toolchain.toml
└── core/
    ├── Cargo.toml
    └── src/
        └── lib.rs

core/Cargo.toml describes the crate:

[package]
name = "plus_out_core"
version = "0.1.0"
edition = "2024"

[dependencies]

edition = "2024" says which edition of the Rust language the crate is written in. Editions let Rust improve without breaking older code. [dependencies] is where a crate lists the libraries it uses. Ours uses none yet.

Go deeper: where do libraries come from?

Most Rust libraries live on crates.io, Rust’s public registry. To use one, you write its name and a version under [dependencies], and cargo downloads it the next time it builds. Bevy arrives this way in chapter 5.

A version like "0.20.1" doesn’t mean exactly 0.20.1. It means that version or any newer one that’s meant to be compatible with it, so cargo may pick 0.20.2. The version it picked goes into Cargo.lock, and later builds keep using it until you run cargo update. That’s why two people with the same Cargo.lock build exactly the same thing.

core/src/lib.rs is the root of the crate. Everything in the crate starts from this file.

Why not just call the crate core?

Pip, oops

Pip: I tried that once. Without --name, cargo names the crate after its folder, and then it warns that “package name core may be confused with the package with that name in Rust’s standard library”.

Rust’s standard library really does have a part called core, so the longer name avoids a clash. It also reads well where it’s used. In chapter 6, the game’s board module starts with use plus_out_core::CELL_COUNT;, and the crate name says exactly where CELL_COUNT comes from.

Run your first test

cargo new didn’t leave lib.rs empty. It wrote a small function and a test for it. Run the test:

cargo test

The first build takes a few seconds. Then cargo runs the test and reports. This is the part of its output that matters:

running 1 test
test tests::it_works ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

Pip, proud

Pip: Your first Rust test, and it passes! All right, cargo wrote it. We’ll fix that in a minute.

Open core/src/lib.rs and read what cargo wrote:

pub fn add(left: u64, right: u64) -> u64 {
    left + right
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_works() {
        let result = add(2, 2);
        assert_eq!(result, 4);
    }
}

The first three lines are a small function called add, and add(2, 2) gives 4. Functions get a proper look in chapter 2, when you write one of your own. Today the part that matters is the test under it, because you’ll write tests like it in every chapter:

  • mod tests { ... } makes a module, a named group of code inside the crate.
  • #[cfg(test)] is an attribute, a note to the compiler about the item after it. This one says to build the tests module only for cargo test, so the tests never end up in the game itself.
  • use super::*; brings everything from the module around tests into it. That’s how the test can call add.
  • #[test] is another attribute. It marks it_works as a test for cargo test to run.
  • The test calls add(2, 2) and keeps the answer in result.
  • assert_eq!(result, 4); checks that the two values are equal. If they aren’t, the test fails and prints both values. The ! means assert_eq! is a macro, a piece of code that writes code. You’ll meet a few more.
Go deeper: why is assert_eq! a macro?

A Rust function takes a fixed number of arguments, and each argument has one type. assert_eq! needs more freedom than that. It compares two values of any type that can be compared and printed, and it takes an optional message after them:

assert_eq!(result, 4, "add(2, 2) should be 4");

A macro can do that because the compiler expands it before it checks any types. Your line turns into ordinary code that compares the two values and, when they differ, stops the test with a message that shows both.

Give the board its shape

Time to replace the example with the first real piece of Plus Out. The board needs three numbers. They say how wide it is, how many cells it has, and where cell values wrap around.

Create a new file, core/src/board.rs, with these three lines:

pub const SIZE: usize = 5;
pub const CELL_COUNT: usize = SIZE * SIZE;
pub const MODULUS: u8 = 4;

const declares a constant, a value that’s fixed when the code compiles and never changes. Rust writes constant names in capitals, and every constant states its type. pub makes each constant visible outside the board module. Without it, only the code in board.rs could use them.

  • SIZE is the width of the board, and its height, in cells.
  • CELL_COUNT is the number of cells. The compiler works out SIZE * SIZE for us, so nobody types 25 by hand, and nobody forgets to update it.
  • MODULUS is where values wrap. % is Rust’s remainder operator, so (3 + 1) % 4 is 0 and (2 + 1) % 4 is 3. Adding 1 and then taking the remainder by 4 is exactly the game’s wrap-around. Counting that wraps like this is called modular arithmetic. A clock does it too. Three hours after 11 o’clock, it’s 2 o’clock.

Why two different number types?

usize is the type Rust uses for sizes and positions. Rust only lets you look up an item in a list by a usize, so counting cells with one saves conversions later.

u8 holds a whole number from 0 to 255. A cell never holds more than 3, so the smallest type is plenty. MODULUS is a u8 because the cell values it works on are. Rust never mixes number types silently, so a usize modulus would need a conversion at every tap.

How do you number 25 cells?

Row by row, from the top left, starting at 0:

The 25 cells numbered 0 to 24, left to right and top to bottom. Cell 13 is highlighted. It sits in row 2, column 3, and 2 times 5 plus 3 is 13.

Every chapter uses this numbering. Keeping a grid in one list, row after row, is called row-major order. It’s the usual way to store a grid, so you’ll meet it again in your own grid games. A cell’s index is row * SIZE + column. Going the other way, index / SIZE gives the row and index % SIZE gives the column, because dividing two whole numbers in Rust drops the remainder. Cell 13 sits in row 13 / 5, which is 2, and column 13 % 5, which is 3.

Now point the crate at the new file. Replace everything in core/src/lib.rs with this:

//! The rules of Plus Out. The board is 5x5 and holds values 0 to 3. A tap
//! adds 1 (mod 4) to a cell and its orthogonal neighbours. The goal is to
//! clear the board within a tap allowance.

mod board;

pub use board::{CELL_COUNT, MODULUS, SIZE};

Three things happen here:

  • Lines that start with //! document the whole crate. cargo doc turns them into the front page of the crate’s documentation.
  • mod board; tells Rust that the crate has a module called board, and that its code lives in board.rs next to lib.rs. A file isn’t part of the crate until a mod line names it.
  • pub use makes the three constants available from the root of the crate. Code outside the crate writes plus_out_core::SIZE instead of plus_out_core::board::SIZE. The board module itself has no pub, so outside code sees exactly what lib.rs chooses to show, and nothing more.

Pip, thinking

Pip: Think of lib.rs as the table of contents of the crate. It lists the parts and says what the outside world gets to see. The real work happens in the other files.

Test the rules

Constants that nobody checks can quietly go wrong. Add a test module at the bottom of core/src/board.rs:

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn the_board_has_25_cells() {
        assert_eq!(CELL_COUNT, 25);
    }

    #[test]
    fn a_value_wraps_from_3_back_to_0() {
        assert_eq!((3 + 1) % MODULUS, 0);
    }
}

Each test’s name says which rule it checks. When one fails, the name alone tells you which rule broke. Run them:

cargo test
running 2 tests
test board::tests::the_board_has_25_cells ... ok
test board::tests::a_value_wraps_from_3_back_to_0 ... ok

test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

The names now start with board::tests::. That’s the path to each test, the tests module inside the board module. Your two ok lines may come in the other order. cargo runs tests at the same time, and whichever finishes first is printed first.

What does a failing test look like?

Let’s find out on purpose. In board.rs, change MODULUS from 4 to 5 and run cargo test again. One test fails, and cargo explains why:

test board::tests::a_value_wraps_from_3_back_to_0 ... FAILED

...

assertion `left == right` failed
  left: 4
 right: 0

left is the first value you passed to assert_eq!, and right is the second. With a modulus of 5, (3 + 1) % 5 is 4, and 4 is not 0. Change MODULUS back to 4 and run cargo test once more. Both tests pass again.

Pip, surprised

Pip: Breaking your own code on purpose feels odd the first time. But a test you’ve never seen fail might not test anything at all. Now you know these two do their job.

Tidy up

rust-toolchain.toml installed two more tools. Run them both:

cargo fmt
cargo clippy

cargo fmt rewrites your code in Rust’s standard layout, and it prints nothing when it’s done. cargo clippy looks for common mistakes and clumsy code. On our code it only reports that it finished, which means it found nothing to complain about. Running both before every commit keeps the code easy to read.

If you’re using git, create a file called .gitignore in the plus_out folder, with this single line:

target/

The target folder holds everything cargo builds, and cargo can build it again at any time, so it doesn’t belong in git. Cargo.lock does belong in git, because it records exactly what went into the build. Now commit:

git add .
git commit -m "Set up the workspace and the rules crate"

Make it yours

Two small challenges to try on your own. Chapter 2 works the same with or without them.

  1. Predict a failure. Change SIZE to 6. Before you run cargo test, guess which test fails and what left and right will say. Then run it, and change SIZE back to 5.
  2. Write a test of your own. Add a test to board.rs that checks the cell numbering. Cell 13 sits in row 2, column 3, and row 2, column 3 is cell 13.
Show the answers
  1. the_board_has_25_cells fails, because CELL_COUNT is now 6 * 6:

    test board::tests::the_board_has_25_cells ... FAILED
    
    ...
    
    assertion `left == right` failed
      left: 36
     right: 25

    The wrap-around test still passes. It never uses SIZE, so the size of the board doesn’t matter to it.

  2. Here’s one way to write it:

    #[test]
    fn cell_13_is_in_row_2_column_3() {
        assert_eq!(13 / SIZE, 2);
        assert_eq!(13 % SIZE, 3);
        assert_eq!(2 * SIZE + 3, 13);
    }

    A test can check more than one thing. It passes only when every assert_eq! in it holds.

Recap

  • rustup installed Rust, and rust-toolchain.toml pins the project to Rust 1.100.1.
  • The plus_out folder is a workspace with one library crate, plus_out_core, in the core folder.
  • board.rs holds the board’s three constants. lib.rs names the module and re-exports the constants with pub use.
  • Tests are functions marked #[test] that check values with assert_eq!. cargo test runs them, and you’ve watched one fail and pass again.

The finished code for this chapter is in steps/01-set-up. If something behaves differently on your machine, compare your files with those.

Pip, proud

Pip: Nice work. Your tools are ready and the rules crate is alive. In the next chapter the board gets real, with a Board type that holds 25 cells and knows what a tap does.