A very tiny terminal snake game, purely implemented in Rust.

Overview

tiny-snake.rs

A very tiny terminal snake game, purely implemented in Rust.

snake-480

Features

  • Optimized binary has only 2760 bytes.
  • No dependencies. Not even libc.
  • Works on x86_64 Linux 3.17+.
  • 100% Rust 🦀.
  • Panic-free.

Background

While thinking about implementing the snake in the bf lang using only ANSI escape codes, I decided to build a proof of concept for the snake logic, and ANSI rendering.

I have also seen this blog post about writing a Hello World program using raw syscalls, and I figured out that is all I would need to write the snake game. I also took the opportunity to implement it in a single file, using rustc directly, and avoiding cargo boilerplate.

In the end, for my delight, I discovered that after building for release and stripping symbols, of the binary was less than 20 KiB. So I decided invest a little more in decreasing the binary size.

Changing some parameters parameters for compilation decrease the total size to about 7KiB, and after making the program panic-free the total size decreased to less than 3KiB.

There is still room for improvements, but the last change was so dramatic that I am now satisfied.

Building and Running

To build the game binary you only need rustc and make:

$ make snake

This creates a binary called snake in the current folder, and you can run it with:

$ ./snake

You can also build and run in a single command:

$ make run

The Makefile contains the following commands:

  • snake: build the main binary.
  • snake.debug: build the a version of the binary with debug info.
  • size: print informations about the size of each symbol and section, and total size of the binary.
  • snake.asm: emmit a clean up version of the assembly emmited by rustc.
  • gdb: run snake.debug on rust-gdb.
  • objdump: produce a clean up disassembly of snake.debug.
  • fmt: format snake.rs
  • clean: delete all generated files.

Acknowledgments

  • Kpcyrd's blog post about writing a Hello World program using only raw syscalls.
  • Graham King's blog post that lists many techniques for reducing the size of a Rust binary, although I still didn't use all of them.

License

This project is licensed under the Unlicense - see the LICENSE file for details.

You might also like...
A very basic show-case of rust on the esp32 in 2022

Readme This example code does the following: Set up a WiFi connection on the ESP32-C3 Spawn a thread using std::thread in which we listen for incoming

F-Fetch targets low systems. Written in Rust. It's very simple, designed so you can pick it up and replace it.

F-Fetch F-Fetch targets low systems. Written in Rust. It's very simple, designed so you can pick it up and replace it. First Look ~/.config/ffetch/con

A very simple third-party cargo subcommand to execute a custom command

cargo-x A very simple third-party cargo subcommand to execute a custom command Usage install cargo-x cargo install cargo-x or upgrade cargo install -

A very opinionated, zero-configuration shell prompt
A very opinionated, zero-configuration shell prompt

A very opinionated, zero-configuration shell prompt

Fls - Ferris-LS, a very bad LS replacement. Besides that, proves that I suck at clean & good code.

FLS A handy ls remake, purely for learning. Why FLS? There's no reason, at all, don't use it. I just want to learn Rust :D Usage Flags: -i = Use icons

dlfile is a very simple, straightforward CLI to download a file

dlfile dlfile is a very simple, straightforward CLI to download a file, except it... Only downloads over HTTPS Requires TLS 1.2 or higher Executes in

Work out how far apart things should be (very quickly)
Work out how far apart things should be (very quickly)

Kern determiner Work out how far apart things should be (very quickly) kerndeterminer is a Rust-based library for determining a kern value between two

A terminal ASCII media player. View images, gifs, videos, webcam, YouTube, etc.. directly in the terminal as ASCII art.
A terminal ASCII media player. View images, gifs, videos, webcam, YouTube, etc.. directly in the terminal as ASCII art.

Terminal Media Player View images, videos (files or YouTube links), webcam, etc directly in the terminal as ASCII. All images you see below are just m

ask.sh: AI terminal assistant that can read and write your terminal directly!
ask.sh: AI terminal assistant that can read and write your terminal directly!

ask.sh: AI terminal assistant that read from & write to your terminal ask.sh is an AI terminal assistant based on OpenAI APIs such as GPT-3.5/4! What'

Owner
Rodrigo Batista de Moraes
Computer Engineering student at Federal University of Itajubá (UNIFEI)
Rodrigo Batista de Moraes
Simple system monitoring app that runs on terminal. Made purely with Rust.

What is it? RCTOP is a simple WIP system monitoring app that runs purely on terminal and doesn't feature GUI. One can compare it to htop, but more str

Niko Huuskonen 7 Oct 14, 2022
An feature packed Google Tasks CLI written purely in Rust

rChore A feature packed unofficial Google Tasks CLI to boost your producitvity, written purely in Rust. ?? What is rChore? rChore is an unofficial Goo

Hemanth Krishna 41 Dec 24, 2022
Emulates an Edge hardware-based room sensor client purely as a CLI application

ambi_mock_client Usage You must have Rust installed to build ambi_mock_client. You can find documentation on installing Rust here. Using cargo run > c

Jim Hodapp 1 Jan 22, 2022
Tiny CLI tool that helps to visualize iCal file content in the terminal.

Calio Calio is a tiny CLI tool that helps to visualize iCal file in the terminal. Installation You can either install it via cargo or download the bin

Oscar Cortez 5 Jun 12, 2023
A tiny library and application for animating text in the terminal.

?? snailshell A tiny library and application for animating text in the terminal. ?? It's so easy to add some flair to your text RPGs or console progra

null 45 Mar 5, 2023
Rust Crate that allows to do interruptions in console. Will be implemented to functional terminal customization kit.

termpause Rust Crate that allows to do interruptions in console. Will be implemented to functional terminal customization kit. Usage Add this in your

Just Said 4 Sep 21, 2022
A very fast implementation of tldr in Rust

A very fast implementation of tldr in Rust: Simplified, example based and community-driven man pages.

Danilo Bargen 2.9k Dec 31, 2022
A cli based pastebin in Rust, but very insecure

pasta A cli based pastebin in Rust, but very insecure Use nightly toolchain to build rustup override set nightly When this program is running, you can

Snehit Sah 3 Mar 25, 2022
Learning Rust through Advent of Code 2021 - probably not very clean!

AoC 2021 ======== I'm using AoC2021 as an excuse to learn Rust (and maybe some other languages). Please do *not* use this repository as a good source

Andrew Zhu 0 Dec 8, 2021
🐱 HQ9C is a very serioues compiler for HQ9+, it meta-compiles with Rust.

HQ9+ Compiler HQ9c (Or HQ9+ Compiler) is a blockchain-based NFT minting AI machine-learning cloud infraestructure for the compiling of the great progr

Alex 5 Aug 28, 2022