Why Rust for game dev - performances aside


#1

I happen to know a little Rust, so I used it for a couple of little projects of mine. The last of these is a small turn-based strategy game, which (at the moment) is not supposed to use any fancy thing like 3D graphics or even animations.

So I am wondering: since neither performances nor safety are concerns in this use-case, is there still any good reason to choose Rust as a programming language? (I mean, for a generic user. For me the obvious reason is that Rust is the only language I know well enough at the moment.) In particular, wouldn’t a higher-level language (maybe Phyton?) speed up implementation times?

Small rant: As for me, there is something I am really struggling with in Rust: type abstraction. I tried using traits and trait objects, but this constantly leads to the compiler complaining about “weird” errors, especially those involving the Sized trait and lifetimes. Indeed, the reccommandation I have received multiple times is to keep things simple, use enums instead of trait objects, etc… Would the situation improve by using a different programming language?


#2

I would say that the key defining characteristics of Rust for gamedev are that it’s strongly and statically typed, with a focus on highly optimized AOT compilation and low-level control:

  • It’s good because it gives structure to your program and makes it easy to understand, which is more and more important as your game code becomes complex, all the while giving your compiler’s optimizer plenty of valuable information. It also catches a lot more errors at compile time, which is that much less time spent on debugging.
  • It’s bad because you spend a lot of time juggling with the type system and waiting for long compile times, which could have been better spent quickly experimenting around with fun gameplay ideas. And dynamic constructs like trait objects get clunky because you can’t just put everything on the heap, duck-type all the things, and stop caring.

For a simple game (say, < 10 kLOC) where performance doesn’t matter, Python + PyGame might be a better option, due to the joy of the fast iteration cycle. You’ll get half of your lines of code wrong the first time due to the dynamic and overly permissive type system, but that’s okay, you can just continuously run the thing, write lots of tests, and fix what breaks, even in the REPL while the game is running if you like. The fun is worth it.

For more complex and ambitious game projects, or if performance starts to become an issue, I would go for a strongly typed language (not necessarily an AOT-compiled one like Rust or C++, fast JIT-compiled bytecode as used by Java or C# is quite capable already) just because all the static analysis and the mental structure provided by the stronger type system becomes invaluable when you need to manage a large software project.


#3

I agree with @HadrienG’s points above. A couple things I’d add.

For simple game programming in Rust check out the following two packages:

Both make it pretty simple to start writing a simple draw/update loop for a game.

If you’d like to see a full example using piston, you can check out an Asteroids game I wrote with a few others: rust-belt

In my case, I wrote the game to learn Rust and enjoyed the experience.

If you want to go even higher than something like PyGame, you could use something like GameMaker.

At the end of the day, find something you enjoy working on (even if it’s not Rust) and have fun.


#4

Thanks for the comments, you made interesting points!

To be clear, I am not looking for a replacement to Rust for my projects (I just do this for fun in my spare time, and Rust is currently working for me). I just wanted to know whether Rust is an arguable choice for my use-case or is just a bad match.

I have already and I got nice results!


#5

Have you thought of using Ocaml?


#6

I think @HadrienG made great points. I suppose it’s a bit difficult to say whether something is a “bad match” without knowing what we’re matching against :slight_smile:. You did mention performance insensitive games as a use case. That doesn’t rule out Rust obviously but it does rule in other languages. At that point the question comes down to what you value in a language. Given you’re most comfortable with Rust you may not have enough mileage to be able to answer this for yourself. As such, I think writing a game in Rust and then writing the same one in some other language will be a valuable developmental experience for you. That’d be my suggestion :slight_smile: