Jared McFarland

Programmer, feminist, classical guitarist, cyclist. Person.

Read this first

Creating a PHP extension in Rust

UPDATE: A few hours after posting the initial draft of this I realized my PHP benchmark was broken. I’ve since updated the PHP and Rust versions to be more fair. You can see the changes in the GitHub repo (link at the bottom).

Last October I had a discussion with one of my coworkers at Etsy about how writing extensions to interpreted languages like PHP, Ruby or Python should be a lot easier than it is now. We talked a bit about how one of the barriers to successfully writing an extension is they’re generally written in C, and it’s hard to gain confidence in C code if you’re not an expert at the language.

Ever since then I’ve toyed with the idea of writing one in Rust, and for the past few days have been playing around with it. I finally got it to work this morning.

 Rust in C in PHP

My basic idea was to write some Rust code compiled into a library, write some C headers for it, use it

Continue reading →

Roguelike Tutorial in Rust: Part 5

This is Part 5 in a many part series on how to make a roguelike game in Rust. If you’re lost, check out the Table of Contents to figure out where you should go.

 Combat! Part III

Last time we made a ton of progress on combat. We’ve got a monster on the screen; we can issue combat instructions; we’ve got different parts of the screens reserved for different types of output. Things are looking pretty okay. Let’s keep going!


While I was working on Part 4 I saw a guide go into pull request on the Rust GitHub. The guide was The Rust Crates and Modules Guide and it was super duper exiting. I’d been really curious about how to do this The Right Way™ for awhile, and I was getting tired of this in my tab bar:

mod mod mod

It was getting confusing!

After reading the guide I learned that modules don’t need to live in a folder (like src/game/mod.rs), they can just be named after the module! So

Continue reading →

Roguelike Tutorial in Rust: Part 4

This is Part 4 in a many part series on how to make a roguelike game in Rust. If you’re lost, check out the Table of Contents to figure out where you should go.

 Combat! Part II

Last post was a bit of a doozy. I’m really hoping we actually get to implement some combat this time. Thinking through what we need for combat I came up with the following rough list:

  • input characters for attack (/ for sword, ^ for boomerang, * for bomb and % for lettuce)
  • the ability to halt the normal input -> update -> render cycle
  • the ability to take additional input (ie, “Which direction are you using your sword?”)
  • the ability to prompt the user and display messages

That’s a lot of requirements. Fortunately, we don’t have any refactoring to start with in this part. The first three in that list are really closely related, so I’m gonna leave them for later. Let’s do the last bullet first, “The ability to

Continue reading →

Roguelike Tutorial in Rust: Part 3

This is Part 3 in a many part series on how to make a roguelike game in Rust. If you’re lost, check out the Table of Contents to figure out where you should go.


Things are progressing nicely! We have rabbit that we can control, and she has a sweet dog that wanders aimlessly. The goal of this part will be to get some enemies on the screen and get our rabbit to fight them.

But before we get started on that, we have some chores to do. There’s some things about our code base that are a little sub-optimal right now.

 int or i32

First, and probably the easiest, it’s good practice specify our integer lengths whenever possible. That means not using int in favor of i32 or u32. We only use int in a few places, and I think i32 will work for all of them, so go ahead and make that change in character/mod.rs, npc/mod.rs and util/mod.rs. I ran into a small snag when making this change

Continue reading →

Roguelike Tutorial in Rust

 Part 0: Why

In mid-April of 2014 I was stuck in a hotel room in Manhattan with the stomach flu. In a fever induced stupor, I started reading Steve Klabnik’s Rust for Rubyists. It’s a great primer for the language, and if you haven’t read it, you should stop here and go check it out. It took me about a half-day in my bed-ridden state to get through it.


In it, Steve talks about _why’s Dwemthy’s Array. His book doesn’t have a complete implementation of the array (and because progress has been diverted to the official Rust Guide, I don’t think it ever will), but I decided I wanted to make a full version I could actually fight. I wanted my rabbit to get lucky, damnit. It was pretty ugly.

I got bored playing my little

Continue reading →

Roguelike Tutorial Table of Contents

 Table of Contents

  • Part 0: Why
  • Part 1: Setup and First Pass
  • Part 2: Bring Our Heroine to Life
  • Part 3: Combat!
  • Part 4: Combat: Part II
  • Part 5: Combat: Part III

 What is this?

I decided I want to learn the Rust Programming Language and I’ve been looking for an excuse to go through Game Programming Patterns, so I decided to combine the two.

I’m making a Roguelike based on Dwemthy’s Array in Rust and live-blogging everything I learn along the way.

Continue reading →

Roguelike Tutorial in Rust: Part 2

This is Part 2 in a many part series on how to make a roguelike game in Rust. If you’re lost, check out the Table of Contents to figure out where you should go.

 Bring Our Heroine To Life

So far our bold rabbit heroine is just an @ on a screen. To really bring her to life, we need to be able to issue commands (like move, attack, etc) and have the game update the display. Before we get to that a digression…

 Game Loop

At the core of just about every game is the game loop. This is an infinite loop that looks something like this:

loop {

There’s lots written on the internet about the game loop, but a great place to start is Game Programming Patterns - Game Loop. It goes into a lot of great detail about The Game Loop pattern, how to apply it, when to apply it and some of the common gotchas. Fortunately for

Continue reading →

Roguelike Tutorial in Rust: Part 1

This is Part 1 in a many part series on how to make a roguelike game in Rust. If you’re lost, check out the Table of Contents to figure out where you should go.

 Setup and first pass

Here I’m going to actually get started with some code and setup, but first I want to go over my rough plan. The GitHub tag for this part is 1.1.


I’m going to try to condense each concrete topic I learn about into a single post. I’m also going to try and maintain a GitHub Repository for this project, and maintain a tag for every post. The goal there is that you should be able to checkout the repo and follow along by checking out different tags.


You’ll need a few tools to get this working. I’m on Mac, so all my instructions will be Mac based. To my knowledge, all the libraries work on *nix and Windows. Here’s a list as of time of writing:

  • Rust
  • Cargo
  • libtcod
  • tcod-rs


Installing Rust is

Continue reading →

Sprockets with Rails 2.3

At FutureAdvisor our main app is still in Rails 2.3 because upgrading to Rails 3 is kind of a pain. I like to keep a few side projects around, like MyRoommate, to play around with new technologies. I’ve upgraded MyRoommate to use Rails 3.2 and am in love with the Asset Pipeline. So I set out to make it work with our Rails 2.3 app at FutureAdvisor.

Here’s what I’ll cover:

  1. Installing Sprockets and mounting the Rack app
  2. Overriding Rails AssetTagHelper to be aware of Sprockets specific pathing
  3. Precompiling assets for use in production (and Sprockets manifest files).

Obviously, the first step was Google. I came across this question on StackOverflow which mentioned this blog from PivotalLabs.

The blog was a bit dated, from December of 2011, and it didn’t work quite right. The first steps were spot on though.

Install the gems (hopefully you’re using Bundler):

# Gemfile

Continue reading →

Rails Routing Internals

Lately I’ve been trying to figure out D (the programming language). I grew up on PHP and Ruby, so I’m not used to compiled languages. I decided I’d make an API (for use with an Ember app) and learn D in the process. First thing I did was check out vibe-d. Vibe-d is a web server and framework for D. I’m not a fan of Vibe route definition so I decided to try and reimplement Rails routes. Along the way I learned a ton about how Rails routing works.

The problem I was trying to work around was how Vibe-d implemented their router. There’s an object, UrlRouter which you use to define routes, with a path and a pointer to a method.

void userInfo(HTTPServerRequest req, HTTPServerResponse res) {
  auto username = req.params["user"];
  render!("userinfo.jd", username)(res);

void addUser(HTTPServerRequest req, HTTPServerResponse res) {
  enforceHTTP("user" in req.post, HTTPStatus.badRequest,

Continue reading →

Subscribe to Jared McFarland

Don’t worry; we hate spam with a passion.
You can unsubscribe with one click.