« All Articles

Rust as a Universal Runtime

Like most developers, I’ve been fascinated by Rust, primarly becuase of it’s promise to eliminate or greatly reduce a certain class of bugs, but secondarily because of the hype surrounding it.

Now, I’ll just say about the first point, I think that writing memory-safe Rust, while a lot easier than C, is still extremely tedious. I don’t think that everything should be rewritten in Rust, especially if the inherent risk is low. I.E., the things a hacker could do with the program are limited. So if you’re writing an OS kernel, use Rust. If you’re writing a 3D modeling program, C++ is probably better.

The second reason, the hype around Rust, made me jump into trying to build various apps and projects with it. And I noticed something that everyone else who’s tried Rust already knew: The tooling, from the compiler to the package manager, is really good. No, seriously. It’s the best I’ve seen outside of the dynamic programming languages (it’s hard to compete with Ruby, Python, Javascript, etc.)

And it isn’t just the tooling that’s good, the ecosystem is great too. The community is busily adding new packages for everything you could ever want. For example, just for fun I decided to make a spinning 3D-rendered triangle in Rust. On a M2 Mac Mini. Can Rust do that? You betcha. Here’s what my dependencies list looks like in cargo.toml:

[dependencies]
cocoa = "0.25.0"
core-graphics-types = "0.1.2"
metal = "0.26.0"
objc = "0.2.7"
winit = "0.28.7"
png = "0.17"

No messing with include paths like C/C++. No debugging obscure libpng errors. It all Just Works. This eliminates a lot of setup time when creating a new cross-platform C/C++ program. Everything is neatly packaged for your use.

There’s just one problem: Many programmers don’t want to take the time to learn Rust. And many who know Rust don’t want to take the time to write a program in Rust. Is it really worth writing your app in Rust, just because the tooling is good? Ah, but this is where things get truly awesome: You can simply embed a dynamic language into your Rust binary. One of the best embeddable languages for Rust is Rhai. Check it out.

So here’s why the ideal app would look like:

  1. Rust is the base layer of your app, you hire a few Rust devs to maintain it and provide langauge bindings to Rhai.
  2. Your core app logic is all written in Rhai

(Or substitute Rhai with Lua, Python, etc.)

This isn’t really a new idea. In fact many commercial apps over the years (including early versions of MS DOS, MS Word, Mozilla Firefox, and I believe various Adobe products) were originally written using this two-part design: Low-level C (or assembly) providing a cross-platform substrate, and a higher-level “scripting” language actually making up the bulk of the app logic. It’s an idea that comes and goes, and I think that Rust is enabling it once again.

(I should note that Golang is also gaining support as a cross-platform powerhouse, simply becuase Go has a large standard library and cross-platform builds out of the box. Maybe I’ll revisit this concept in another blog post, about Golang).

Published Mar 10, 2024

Software creator, writer, and all around nerd.