Let's Build Snake with Rust

Let's Build Snake with Rust

Learn Rust, the tech industry's most loved programming language by building Snake, the simple but addictive game found preloaded on old Nokia mobile phones.

Since 2016, Rust has been voted the “most loved programming language” every year in the Stack Overflow Developer Survey by what appears to be a growing margin, and after checking it out for myself, it’s pretty clear why.

Rust offers a plethora of features you’d expect from a modern language and addresses pain points that are present in many others. It competes in the same kind of space occupied by C and C++, offering similar performance, but it is also known for safety, reliability and productivity.

The trade-off with Rust is in its complexity, however; it has a reasonably steep learning curve, particularly with regards to its unique memory management model known as ownership.

In this post, we’ll explore a step by step implementation of Snake, the simple but addictive game found preloaded on old Nokia phones. We’ll use the terminal as the UI and the keyboard for input.

I should preface this post by stating that I am by no means an expert in Rust. I’ve been learning it on and off in my spare time for the past year or so. I’ve also more or less thrashed the game out without much thought for code quality, just as a learning exercise more than anything else.

You can find the code for the game on GitHub, where I would also be happy to address issues or make corrections to this post.

Getting started

Let’s create our project using Rust’s build system and package manager, Cargo.

$ cargo new snake-rs

This command creates a new Rust project called “snake-rs”, in a folder of the same name in the current directory. It creates a ~/Cargo.toml file and a ~/src/main.rs file inside that folder. Cargo.toml is a bit like package.json for JavaScript/NPM based projects, or pom.xml in a Java/Maven project and the main.rs contains a main function which is the entry point to the application.

fn main() {
    println!("Hello, world!");


Let’s start with a few building blocks for the game.

The Direction Enum

The snake will always be travelling in one of four different directions, so we’ll use an [enum](https://doc.rust-lang.org/book/ch06-01-defining-an-enum.html) to define the Direction type:

// src/direction.rs

#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum Direction {

Enums in Rust are just like those found in Java; they define a fixed set of possible variants for a value. In the code above, the pub declares that the enum type is public; in Rust everything is considered private unless explicitly stated otherwise.

#[derive(...)] is an attribute (annotation) which automatically derives implementations of some traits for the type.

trait in Rust is similar to an interface with default methods in Java 8+. It is an abstraction which can declare methods that either contain a body, or must be implemented by the concrete type.

The traits named in the #[derive(...)] attribute come from the Rust standard library and they each have a special meaning. By using this attribute, Rust automatically makes our struct implement those traits and provides their implementations.

jhenderson tech

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

The Biggest HR Tech Conferences in 2020.

As the HR tech market is booming, experts in the industry share their insight at the biggest HR tech conferences in 2020.

HR Tech Market—What You Need to Know About It

HR tech is nowadays common in most enterprises, growing faster than ever. Find out what drives this market and why it is worth exploring.

Remote Check-Ins, AI, Apps: Tech-Enabled Recovery Strategies for Hotels

Remote Check-Ins, AI, Apps: Tech-Enabled Recovery Strategies for Hotels. Technology is ever-evolving and so are user expectations. Technology is also a key element hotels can use to stand out from the crowd, to better assist their guests, and increase revenue. Sales are a key area in which hotels are always on the lookout for innovative methods to increase their profits. 

Berlin as an HR Tech Hub—10 Inspiring Startups and Their Tech Stacks

Berlin is a budding HR tech hub with a growing pool of startups. We’re exploring 10 inspiring Berlin startups and their tech stacks.

The top 5 lessons I’ve learned as a Latina in Tech

Today was a not so good day at work.I’ve had better days. The issue itself isn’t even regarding my day-to-day work with clients or my immediate team. The issue is regarding how one of the largest technology companies in the world fails to understand and account for my personal living situation, during COVID-19. But that’s a whole different story, for another time.