r/rust • • 4d ago

šŸ™‹ seeking help & advice Should I add support for Rust?

Hi, I am the developer of Leadwerks Game Engine. I'm looking for a mid-difficulty language, somewhere in between C++ and Lua. C# would work nicely, but the lag spikes caused by the garbage collection seem like it ruins the language for realtime 3D games, especially for VR. I'm not that familiar with Rust but I hear a lot of good things about it, and it appears to use shared pointers, which eliminates the performance problems full garbage collection would introduce.

Support for Rust can be added by piping all the commands through a C interface and writing some structure/class code on the Rust side that wraps everything up nicely for the end user.

I'm aware of the open-source Bevy 3D engine, but it's lacking a visual editor. Leadwerks 5 has a very capable level editor / visual designer that's been refined over years with lots of user feedback. To be clear, Leadwerks is closed-source commercial software (with a low price point and no royalties).

Do you think Rust would be a good match for what we are looking for in a language? Would adding support for the Rust programming language would be of interest to developers here? Please let me know your thoughts and if you have any questions.

22 Upvotes

50 comments sorted by

44

u/dgkimpton 4d ago

Rust is way closer to c++ than lua/c#. It's not a bad language to support but I suspect most of the people that don't want to use your engine in c++ won't be much more likely to use it in rust.Ā 

-3

u/velocityvector2 3d ago

Rust isn't close to any of them. It’s not even an OOP language. It’s a different approach.

-18

u/Leadwerks 4d ago

I see C# as a sort of sanitized subset of C++, with fast compile times and simpler error reporting. I mean, C++ is just layers and layers of historical cruft stacked together.

How do you think Rust compares to this? Does it provide a more modern simplified developer experience, in the way that C# does?

51

u/Solumin 4d ago

That's not an accurate view of C#. It's most similar to Java, including running on a virtual machine and being heavily object-oriented.

Rust is closer to C++.

-4

u/Leadwerks 4d ago

I mean in terms of the user experience. I know the compiler details are completely different, but I am talking about how the user learns how to program with it. Most of our code examples for C++ work in C# with only minor changes.

10

u/Solumin 4d ago

In that case it sounds like the most important part is the quality of the libraries/API you offer.

If it'll look nice in C++, it'll look nice in Rust.
In terms of actual developer experience... Rust can go either way, depending on what you're doing. You don't have to think about constructors, destructors, or memory management for a lot of programs, but you can think about those things if you need to.

16

u/dgkimpton 4d ago

That is... fundamentally about as wrong as you can get. Yes, they're both programming languages and have braces. Beyond that they are totally different.

C# is much closer to a scripting language due to its garbage collection, runtime reflection, JIT compilation, bytecode runtime, and general ease of use. It can consume C api's if you jump through the correct hoops but it isn't anything like C++. C# also has decades of cruft and multiple ways to do stuff all stacked together.Ā 

Rust is much more like C++ in that it has manual memory management, template style generics, compile to binary, and strict compile time type checking. It also has a borrow checker which is an additional set of hoops to jump through to act as guard rails, but it can be a challenging fit with C api's at times - depends how good your api is. Rust is both harder and easier than C++ all at the same time.Ā 

-2

u/Leadwerks 4d ago

Yes, I understand that, but if I am writing a tutorial about the language the end user doesn't care about all those details. They just need to know to wrap their main function in a class, type a period instead of -> when calling a method, and that C# lists are sort of like STL containers. In practical terms, as far as their usage is concerned, it's basically a simplified version of C++.

7

u/dgkimpton 4d ago

Ok, then from that point of view Rust is probably a more complex C++. C++ let's lots of things just slide that Rust will absolutely force you to reason about and prove before letting things compile (want to hold a pointer after it's valid? Go ahead. Want to use after free? Knock yourself out. Want self-referential data structures? Why not? Want linked lists? Just slap em together. Etc).Ā 

I'd summarise Rust as more up-front effort than C++ for the benefit of not having to worry about or even remember things later.

But easier than C++ is not at all how I'd describe it - without the qualifier of "easier to get it correct".Ā 

1

u/Leadwerks 3d ago

"More restrictive" is how I would describe it, based on your description. I wasn't really thinking about the SC nature of Rust when I posted this thread, but that is kind of interesting to me now, since I use Leadwerks in many of the areas where this is relevant, like self-driving cars, aerospace / NASA, etc.

25

u/Konsti219 4d ago

How to lose all credibility as a "game engine developer"

20

u/Wonderful-Habit-139 4d ago

C# and C++ both have the letter C in them, and C# has one character less than C++, so C# is a subset of C++.

Sounds good to me :p

11

u/Cyan14 4d ago

How are you even making a game engine with that knowledge? 😭

5

u/Kurimanju-dot-dev 3d ago

ā€žClaude make me a gud game engine make no mistakesā€

3

u/catheap_games 4d ago

Rust reputation might be misleading because from the end result perspective (targets, performance), it does get compared to C/C++, but the borrow checker confuses a lot of experienced developers who think that just because they know C++ pretty well, they should be able to use Rust in a week without reading the manual / getting proper training.

Rust is "more strict" in that the design philosophy is to be memory-safe and catch issues at compile time, and have fewer UB and other types of bugs.

You should expect ~3 weeks of full time training to get good at it, assuming you want a solid general knowledge. You can get enough basics to make some things or maybe Bevy prototypes in a few days of learning Rust + a few days of learning Bevy, if you are disciplined and don't tire out too fast.

36

u/NullOfSpace 4d ago

Rust is a good language, but it isn’t a replacement for C#. It’s closer to C++ with most of the footguns removed, and honestly requires you to have a better understanding of what your code is actually doing in a lot of cases (so you can prove to the compiler that what you’re doing isn’t going to cause memory issues), so I don’t know if it fits the bill of a ā€œmid-difficulty language.ā€

That said, it definitely has the stuff you’re asking for from it. The C FFI is good (though you need unsafe blocks, since the compiler can’t guarantee memory safety using it), and it does support basically whatever memory management system you want (in the same way the C++ does, ā€œbuild it yourself,ā€ but with some extra safety guarantees).

6

u/Leadwerks 4d ago edited 3d ago

"Footguns"...hahaha, yes I know these. The safety nature of Rust is interesting, since Leadwerks is used in many of the environments where this is important.

5

u/ElyzraPearl 3d ago

you make rust sound intense, kinda hot how strict it is tho

-8

u/velocityvector2 3d ago

Rust isn't close to C++. Rust is rust

9

u/Recatek gecs 3d ago

Rust's memory model and performance characteristics are much closer to those of C++ than C#.

-7

u/velocityvector2 3d ago

Rust has strict rules regarding memory management and is unlike C++. C++ is an OOP language; you cannot directly port the code you write to Rust. And C++ is a primitive language that doesn't even have a package manager. They are completely different in every aspect.

9

u/xvillifyx 3d ago

You’re conflating features the language has with the resulting behavior of the actual machine code generated

1

u/pleaseihatenumbers 2d ago

most modern c++ code is not OOP and is closer to rust in that sense, which is probably part of why rust opted to not allow OOP altogether (as we have learned from the experience of c++ that OOP in a system's programming language is not such a nice paradigm)

1

u/velocityvector2 2d ago

I just said Rust isn't like C++, I don't understand why I was downvoted. Is that a lie? They are two languages ​​that are completely different in terms of OOP and memory management.

1

u/pleaseihatenumbers 2d ago

rust is absolutely not completely different to c++ in terms of memory management, i would say they are almost the same in that regard. rust has a very similar "mission" to c++ in my opinion, to be a system's programming language with higher level concepts such as powerful generics implemented with very low overhead. i agree that rust is not a c++ successor language but in my opinion the language which precedes rust which rust is the most similar to is c++, and most ways in which they are different are things rust has learned from c++'s mistakes (move semantics, non c-like syntax, doing away with oop altogether, package manager, traits [which c++ kinda copied with concepts] ...)

1

u/IceSentry 1d ago

People are explicitly calling out which parts of rust are similar to c++ and you just ignored it and doubled down on the parts that are different.

9

u/cynokron 4d ago

Is it a good match? Yes. Will rust developers use it? Not sure. A lot of people on this sub like fully rust solutions. C interop has overhead, particularly if you want to convert a rust string to a c string due to not needing null terminator in rust. There is probably a way to workaround it, but i havent bothered. A lot of rust users will have their own engine, or yes use bevy. A lot of bevy users might appreciate the editor, but you'll want to market your engine more than just having an editor.

Being closed source probably kills a lot of interest. Self serve bug fixing and "documentation" are extremely important to development.

2

u/Leadwerks 3d ago

Good information here, but I will point out that string interop will never realistically form a bottleneck for games. You have much bigger problems to worry about!

2

u/cynokron 3d ago

C# is probably more your target then

6

u/stumpychubbins 4d ago

C# has enough tools to control the GC that I’d be surprised if you can’t make it work. I think unity does some funky stuff to translate it to C++ (meaning it would use refcounting instead of a global GC) but even without that you can get pretty fine control over when the GC runs and how it’s tuned

2

u/Leadwerks 3d ago

Is "doing funky stuff" a good strategy? What if we could build on something that wasn't fundamentally broken by design?

3

u/martindevans 3d ago

The "funky stuff" that Unity does is entirely due to legacy design decisions (Mono, Boehm GC) which they are now working very hard to undo with the CoreCLR migration they've been working on for a couple of years. It was a necessary design decision about 20 years ago, but it's unecessary now if you just use modern dotnet from the start!

A good GC is a tradeoff. Allocations are very cheap (just a bump allocator in gen0) compared to Rust (imagine if you used bumpalo for everything). The tradeoff is that an allocation can trigger a collection, which makes some allocations more expensive. Generally you have higher throughput with worse tail latency).

Unity with Boehm GC has none of these advantages, it is the worst of all words! It's conversative (has to scan the entire stack, can discover false roots), non moving (doesn't compact the heap so it can become fragmented), non generational (has to scan the entire heap, has expensive allocations like Rust). So do not take anything away from Unity, other than how not to do it.

[Rust] appears to use shared pointers, which eliminates the performance problems full garbage collection would introduce.

Reference counting is the least performant of all types of automatic memory management. Rust does a great job at avoiding most of the cost of RC by using lifetimes more most allocations. But if you ever find yourself reaching for Rc<T> or Arc<T>, you're in a worse position than using a proper GC.

It's worth pointing out that even allocations managed entirely by lifetimes can absolutely cause stutters. If you drop the root of a large object graph it will immediately walk that entire graph, calling free on every object. This could be significantly more expensive than an equivalent GC collection in the worst case.

Do you think Rust would be a good match for what we are looking for in a language?

Coming back to answer the original question. No, I don't believe so. Approximately nobody in gamedev uses Rust, it's a great language for engine development but a bad choice for your scripting frontend unless you specifically want to be pushing adoption of the language. That's a worthy goal, but know what you're signing up for!

On the other hand C# is a very popular language in games development. The only downsides you list are the largely unfounded fear of GC stutters. This is addressable even in even in terrible state of current-day Unity (as demonstrated by a large number of successful games including very latency sensitive VR titles).

1

u/stumpychubbins 3d ago

No, I don’t think you should do what unity does. Just manually tuning the GC should be enough

3

u/Tall-Introduction414 4d ago

Can I just ask what you mean by "mid-difficulty" language? Isn't it more that you want the control that allows for a low latency pipeline?

I'm probably being pedantic. But I don't know why you would want a language to be "more difficult," except perhaps for novelty. :)

2

u/Leadwerks 4d ago

C++ is pretty brutal. The error reporting is cryptic. Header files are tedious. No reflection means you have to manually write a lot of loading code that is automated in Lua. I would like to offer something that is a step up from Lua but simpler than the user experience in C++.

3

u/xMAC94x 4d ago

Just had a look at your engine and got to say: nice work.

For providing rust-bindings: As a company you probably want to make money and rust is known for a abundance of game engines, but almost no games. So i doubt that it's a good short-term business decision.

Though rust can be an awesome language and might be a good fit for new tooling or parts of the engine one day.

2

u/Leadwerks 3d ago

Thanks, I appreciate the compliment. On one hand, I want people who want to make games to be able to do so, but on the other hand, it's not really my place to tell people how to use our software. Not everyone dreams of being a professional game developer. Many people just like bringing their ideas to life for no other purpose than to have fun.

1

u/catheap_games 4d ago

I can't speak for the market, but if you offered a paid editor with good developer experience for Bevy, I think, depending on the price, some people would be interested. Would it be worth the development time? ...let's be honest, probably not at this point in time. Most "hard core" developers already know what they want, and most other developers prefer the popular and convenient solutions, C#, GDscript, Python, Lua, JS/TS, or visual scripting.

Again, can't speak for others, but for $100, having an editor that would allow relatively fast prototyping, while letting me run the result fully in Bevy, with proper ECS and a full control over the code, that would be a no-brainer purchase. Getting a closed source engine that I can just "script" in Rust? Maybe 3 years ago I'd try, but right now Bevy is getting seriously good for my needs / workflow / game ideas, and if I wanted a quick something with an editor, I'd probably give Godot a go.

Rust ecosystem has a lot of tinkerers, and as others said, "more engines than games" is the classic joke, but fundamentally people who want raw Rust power and pretty good developer ergonomics (sans editor) go to Bevy, and people who want an engine+editor generally split between Unity and Godot, with Godot having the benefit of having an extension that allows using Rust. Maybe that could be a starting point for your research? https://godot-rust.github.io/

2

u/Leadwerks 3d ago

That sounds a lot like my program 3D World Studio. This was the precursor to the full game engine Leadwerks, and was actually used to make the game SCP Containment Breach.

1

u/Present-Knowledge-57 2d ago

I’d personally be interested in it, but I’m not sure if other game developers would be. for a mid-level language wanting to attract the widest audience, I’d consider C# anyway, even despite any alleged GC stutter. in my experience, I did game engine development in Java for months and never noticed any GC stutter that couldn’t be fixed in seconds.

1

u/Leadwerks 2d ago

Thanks for your input, that's useful to know.

1

u/Latin-Freak 2d ago

Have you considered exploring Goose instead? I am not an expert at all, but reading through your issue description I had a feeling that it can be a better fit than Rust, the main reason being that it natively supports C interoperability as Goose code actually compiles to C-like binaries https://aardappel.github.io/goose/

1

u/Leadwerks 2d ago

Never heard of it, but I will check it out!

1

u/exDM69 1d ago

If you write a C interface to your engine it unblocks using it from any language, including Rust, but also Python, Ruby, whatever. C++ interop is much harder from any language.

When you have a C interface, you can build the Rust low level wrapper using bindgen and maybe a few helpful annotations.

But that won't be idiomatic Rust and you'd probably need to add at least some Rust code to benefit from Rust language features like safety. E.g. make a your RAII objects using Drop.

That said, language boundaries always bring impedance mismatch problems and creating an interface that's even close to idiomatic in two languages is a proper engineering challenge.

So yeah, maybe start with the C interface, can't go wrong with that and unblocks all the other languages too. Interfacing C++ and Rust code is a bigger challenge.

0

u/tabbekavalkade 4d ago

Godot had to create their own scripting language for this purpose. I suspect Rust does not fit. It is not a scripting language.

1

u/-Redstoneboi- 3d ago

there is a balance to be had between scripts and full-on performance code. gdscript is useful for most cases, but it is still interpreted and may stop being well-suited for a task if done across 100,000+ or a million entities. imagine if minecraft world generation was in gdscript.

natively compiled scripts are another option to make things run quickly if someone needs to squeeze every bit of performance they can get.

2

u/Leadwerks 3d ago

LuaJIT is a great example of that. :) I looked at the repo recently and I guess the main guy who created that is back at the wheel again.

1

u/TheRealScaramucci 13h ago

IMO C# strikes that balance the best. I think it gives you the best control over memory compared to any other garbage collection languages, excluding a couple of exceptions where the GC is optional (like D).

At the same time you don't need to understand any of that unless you want performance.

Java, often compared to it, doesn't even come close to that level.

-3

u/victor_crimea 4d ago

Don't you expect humans hand writing code with your framework? Optimize for ai generation. Thus Rust is the best choice since it provides the best guardrails to agents.

2

u/Leadwerks 3d ago

In my opinion, the jury is still out on the usefulness of LLMs in writing code. I'm open to the idea of integrating code assistance, but I have so far found these tools to be of very limited utility for my own purposes. Discussions with current users indicate they overwhelmingly prefer locally run models.

At this point in time I would not make API design decisions based on their suitability for AI usage.