Photo by Jason Leung on Unsplash

Member-only story

How to Build a Chequers Game in Rust

Byte Blog

--

This article is open to everyone, non-members can access it via this link

Welcome to the world of Rust, where safety isn’t just a feature — it’s a lifestyle! Ready? Brace yourself for the glory of enums, structs, error handling, and ownership…and, of course, you’ll get the entire code at the end. Let’s jump in…

Step 1: Setting Up Your Army

Rust’s enums are powerful — and we’re going to use them to capture the essence of chequers pieces.

First, we’ll define our types of pieces and their colour:

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum PieceType {
Regular,
King,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Color {
Red,
Black,
}

Enums here let us define exactly what each piece is. And since Rust wants everything to be precise, we derive Clone and Copy to easily duplicate our data when needed. Key takeaway: In Rust, you can control data duplication without side effects like accidental mutation. It’s not just enums—it’s an exclusive club for well-behaved types.

Step 2: Structs — The Battle-Ready Soldier

To build a “Piece” on the board, we combine our enums into a struct. Think of structs as the Rust version of a Swiss Army knife.

--

--

No responses yet