Making a Brainfuck Interpreter in Rust

rust dev.to

Making a Brainfuck Interpreter in Rust

Seeing this title, there are three kinds of people reading it. First: "What is this Brainfuck, is this guy cussing or something?" Second: people who know what Brainfuck is and are impressed someone worked with something that complicated. And lastly, the people who actually know it — and are confused why anyone would write a blog post about something so simple.

Honestly? The third group is right. It's genuinely very simple. There are only 8 instructions in Brainfuck:

  • + → increment the value at the pointer by 1
  • - → decrement the value at the pointer by 1
  • > → move the pointer right
  • < → move the pointer left
  • . → output the ASCII value at the pointer
  • , → take input and store it at the pointer
  • [ → if the value at the pointer is 0, skip to past the matching ]
  • ] → if the value is 0, move on; otherwise, jump back to the matching [ And that's it. Genuinely simple to implement — and ironically, that's exactly why people assume it's hard.

What Actually Makes It Complex

Writing even the simplest program takes a lot of instructions. For example, here's "Hello World" in Brainfuck:

++++++++++[>+++++++>++++++++++>+++>+<<<<]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
Enter fullscreen mode Exit fullscreen mode

As for why it exists at all — a group of people once got together and asked, "how small can we make a compiler?" The answer: 170 bytes. Yes, 170 bytes. That's how Brainfuck came to be.

Why I Built This

Moving from history back to the actual coding — I decided to build an interpreter after watching a video of Teej_dv building one for Vim9Script using Rust. And guess which language I happen to be learning? That's right, Rust.

I wanted to learn how interpreters work, and the best way to learn something is through small wins before tackling bigger problems — the same way you'd build a CHIP-8 emulator before attempting a Game Boy emulator.

So I thought about what to build one for. Make my own language? No — way too much work. That's when Brainfuck came to mind. I'd watched a ThePrimeagen video on the topic a few days earlier, and since the instruction set is so small, it felt like the perfect fit.

Building It

The core setup: two pointers, one for the code and one for memory. Implementing +, -, >, and < was straightforward. Same for , and ., with a simple character conversion for the latter.

The real challenge was [ and ]. I needed to track where a [ was located so I could jump back to it when looping, and track the position of the matching ] for skipping forward.

I considered a couple of approaches:

  • Using a stack to store the position of each [, popping it when jumping back from the matching ].
  • Using a counter: starting at i = 1, incrementing on every [ and decrementing on every ]. When i hits 0, you've found the matching bracket and skipped the whole nested block correctly. ## The Performance Problem

Where I ran into trouble was speed. Picture a loop nested inside another loop — even after the inner loop becomes irrelevant, the interpreter kept scanning through it instruction by instruction, which blew up the time complexity.

My code was running painfully slow, so I asked an AI what was going wrong. It pointed out a few issues, and I fixed them — the biggest fix being a hash map to store matching bracket positions directly. That took bracket-jumping down to O(1). The tradeoff: you now need to scan the whole program upfront to build that map, adding an O(n) pass before execution. Worth it.

After that, it ran fine.

Honestly, I had more trouble with my laptop acting up during this than with the actual code.

Would I Recommend It?

Yes. It's a genuinely simple project, but the moment you run actual Brainfuck code through your own interpreter, people will think you're a certified genius. Working on it felt less like "writing code" and more like directly implementing an instruction set — which, in a way, is exactly what it is.

What Does It Teach You?

Honestly? Not much, if you've already been writing Rust for a while and are comfortable with things like match, vectors, saturating_add, and char::from(). This isn't a project that stretches your skills — it's more of a fun, satisfying build.

Could I take it further? Sure — maybe a more advanced version. Call it "Mindfuck" or whatever you like.

That's it for this one. Bye.

Source: dev.to

arrow_back Back to Tutorials