Rust for UE ?

Unreal is 20+ years of C++; not happening.

Ah, yes, Rust … another in a long line of languages created to replace C++. Who remembers Vala, D, Limbo, …

I’m still learning Rust myself. But in general it’s less complex compared with C++ and OOP. Though, for the record, aside C#/F#, I know way more about C than C++.

Rust is more like C, plus functional programming and polymorphism via interfaces called “traits”. When I first saw Rust I actually confused it with MS’s F#. Both are derived from ML and OCaml, etc, too. But to be clear for those who don’t know yet, rust is native code, not managed. It’s a “system language” used for anything really, game engines, embedded, and even operating systems.

All variables are immutable by default. There are no classes in Rust. You only get structs and enums. And enums can hold data. No headers.

Yes, you can define methods/member functions even with structs. Every “implementation” lets you define a bunch of methods for a struct as if it was a class. “Trait” definitions are like interfaces, and define method signatures that can be shared across structs implementations.

Yeah, it looks like the best of OOP but without the worst of OOP like inheritance. Class types may not be the worst, but they are totally useless as Rust proves it that mere structs work, too, when you “separate data from behavior”. Yes, you can do a more data oriented approach in C++, too, but Rust does that naturally.

Some say that C++ got a bad reputation not because it is so bad, but because new programmers abuse it that bad. Shows that C++ is more complex than Rust out of the box while doing the same thing. Rust wouldn’t allow you to do any other way.

And of course we use pointers as usual, but they are checked at compile time with the “borrow checker” that defines boundaries and memory owner ship that can at run time be auto freed when out of scope. It’s like a GC, but it’s at compile time GC, which makes Rust virtually as fast as C++ without the GC hiccups at run time that you can find in C#/F# yet still being at the same memory safety levels as managed code languages.

Coupled with immutable values you get thread safety, but no undefined behavior, no dangling pointers, no seg faults, no buffer overflows, no memory leaks, etc. There are no nulls in Rust. So no run time exceptions because of that either. All this is the stuff of nightmares in C/C++ no matter how hard you try or skilled you are.

But, you can still go “C” in Rust with the “unsafe” keyword code block/functions that can be inside normal Rust code anywhere. Though, it’s C when you use C APIs. Otherwise unsafe code is used to do very low, and specific hardware programming, or just some performance optimizations that allow you to use pointers in exotic ways but without the compiler throwing an error that would prevent your project form successfully building.

There are other features, too. You won’t find silly C like:


for (int i; i < 100; i++)

Instead you do this.

Rust uses also “functional programming” paradigms, but that’s not really new to C++ either. I just mention it because there is value in FP if done on a middle ground level. F# may go a bit overboard with FP. Even the signatures in F# are “->” math like single parameters that can make you wonder what the heck when you miss one parameter and the entire signatures changes as the compiler tries to infer the type. F# got the monad in its own way. And fanctors. So, F# got a bit too much of FP if you ask me. I’d rather use Rust that is more C like. But heck, F# is a very clean language. It’s just too bad that it’s also managed code like C#.

But for the record regarding managed code performance. The game Space Engineer was written fully in C#. It’s not too bad, but I’m not under the impression that Rust is much harder than C#. So, why bother with C#? I see people using C/C++ for GUI, let that sink in…

Well, ideally it should have been C/C++ that should have evolved this much, and they do evolve but so slow that it always allowed yet another language to spawn in the first place.

C++ got rid off the headers only now. And it’s still an unsafe language.

Rust got “editions”. Current is 2018, seems Rust can introduce breaking features much faster than other languages because of that. Internally it got three intermediate layers. Any binary compiled in 2015 edition can mix with 2018 and so on.

The IDE can be shallow for Rust. I use CLion with a Rust plugin, but the IDE support got better. And currently there is a lot of push for IDE support because soonish VS 2021 may be here. And the Rust language team clearly understands that it needs the support of MS because it ain’t just about the language itself. It’s also about jobs, so programmers having access to that language on a professional basis. MS got a lot of jobs internally. So, this isn’t just about VS supporting Rust either. I don’t even user VS, I use CLion.

The video below compares C++ vs Rust on Polymorphism. It’s a bit older though, it doesn’t include C++ 20.

In short, Rust is more compact and more accessible: https://www.youtube.com/watch?v=VSlB…&index=17&t=0s

[TABLE=“align: center, border: 1, cellpadding: 1, width: 500”]

C++
Rust

Overloading
Traits

Templates
Traits

Concepts
Traits

Virtual Functions
Trait Objects

Virtual Concepts
Trait Objects

Variants
Enums

C++98, C++03, C++11, C++14, C++17, C++20, C++23, C++26, …
C++ is constantly evolving, and the best of all: backwards compability.

Someone once said:

Why Rust will never replace C++? Because Rust doesn’t provide us with a slow and adaptive transition, like C -> C++.

And so, Rust will have the same fate as every other pretender.

Rust does provide an adaptive transition. Rust is very compatible with C. It’s just C++ that may need extra work (it always does, doesn’t it?). And not everything is written in C++.

Many crucial MS APIs like DirectX are under the COM interface that are language neutral. DirectX is owned by MS.

Yeah, it’s currently uncertain who is going to make Rust bindings for DirectX, whether it be MS or the Rust community again, but the COM Rust bindings generator is currently made by MS. We will know once VS 2021 is out in preview, and MS made their final choice to embrace Rust. With a giant like MS behind Rust, Rust won’t go back into its corner that fast if at all. And of course that’s important for those who already chose Rust already, too.

“We are the Rust language. Lower your shields and surrender your code. We will add your jobs and technological distinctiveness to our own. Your culture will adapt to service us. Resistance is futile!”
– At Microsoft Build 2021

Jokes aside, if Rust can’t dethrone C/C++, then no language ever will. Which is fine if it manages to evolve fast enough, but the race is on.

I’m not aware that there ever was a bigger threat to C/C++ than the Rust language. One subjective view tells me that there are a large amount of Rust presentations I came across. The more actual fact that gives that away is its 4 years long streak in being the most loved language on Github and the large code base on Crate.io. Add a thousand Rust jobs at MS with DirectX bindings and the Rust written game engines will be coming ringing in. Yes, there are DX bindings in the Winapi Crate already, but they are incomplete and handwritten.

I can’t tell how many companies are behind D, Limbo, Vala etc, but Rust got many of production grade users.

It actually may to a large extend.

It’s just that normally a language may not be so obviously superior to another. It may be up to subjective debate whether specific features are better or worse. And all you get is a syntax war in the forums defending their religion or reasonability though, trying to avoid having to learn yet another language.

However… we are not comparing C# vs F# here. We are comparing C/C++ with Rust, both are system languages a.k.a. native code languages, but one of them isn’t a safe language.
We are not comparing the lack of a semicolon in F# vs the bracket kingdom of C#. And that’s not giving enough credit to what F# brings to the table. Just my opinion, anyways.

But we talk about: Unsafe threads, seg faults, null exceptions, dangling pointers, memory leaks, etc. That kind of stuff of nightmares that is more than just syntax inconveniences, and can be avoided fully just by switching languages.

Regarding stability Rust certainly adds a massive ton to the code. With Rust you get a measurable increase in stability. That’s why MS is willing to invest a ton of money and bring Rust to their ecosystem instead of hiring better C/C++ programmers that could have avoided their “70%” of security issues. That’s not a trivial task for a company as big as MS, it ain’t done out of shear fun. And the security issues are driven by the safety issues of C/C++, it’s an unsafe language. It’s that simple in the end.

It’s like arguing that native code is faster than managed code, uhm, duh? Rust is faster than F# in general, and especially in real time applications.
Yes, Rust is a safe language. No, C/C++ isn’t a safe language. So Rust will deliver stability out of the box while C/C++ won’t.

It’s just companies like MS who got security issues to deal with that can get into the millions/billions when exploited by a hacker. That are the first to point out that C/C++ lacks safety and needs to improve or risk getting replaced by another language that is safe. Like Rust.

But to be clear. The security issues are driven by the lack of safe code, so how C/C++ handles, or rather doesn’t handle its pointers. What ever crimes C/C++ may have committed with its syntax, or bloat, the biggest issue is safety even for non security critical applications. Getting a seg fault, undefined behaviors, memory leaks, etc, ain’t “stability” it’s a waste of productive time because you are using an inferior language.

Like I said before. It would have been obviously way more ideal for C/C++ to evolve instead. But it seems that it just does so too slowly. Otherwise MS would have waited for a better version instead of considering to ditch C/C++ all together in favor of Rust and spending a ton of money and effort on reforming their giant empire.

First of all there is no C/C++ language. Quick example: VLA is legal in C, but illegal in C++. This is the main reason why C++ is being hatred. Because it is taught as “C with classes.”

I highly recommend this video: https://youtube.com/watch?v=YnWhqhNdYyk

In modern C++ we have smart pointers and RAII. Using raw pointers (with raw new/delete) is antipattern. UE4 use raw pointers heavily.
In modern C++ we avoid macros because we have type safe constexpr and templates. UE4 uses macros heavily, UObject cannot be templated.
In modern C++ we prefer composition over inheritance and generic programming. UE4 is “inheritance-everywhere”
In modern C++ raw loops can be replaced with range-for or even better with algorithms/std::ranges.
C++20 introduce modules, so headers can gone.

OOP generally isnt bad, its simply misunderstood. Like Alan Key, considered as a father of OOP says:
“I’m sorry that I long ago coined the term “objects” for this topic because it gets many people to focus on the lesser idea. The big idea is messaging.”

As you can see, these are not problems of the language itself, but flaws of UE4 codebase. Introducing a new language will not make them magically disappear.

So, to what extend is modern C++ as safe language? Rust is 100% safe. Well, maybe make it a 99%…

And for the record, since you mentioned how UE4 is written in the first place. WinRT is also written in a object oriented way. It got Win32, so C in it, too. However, unlike C++/WinRT, Rust/WinRT is still 100% safe according to MS no matter what the actual underneath code is. The same with C# for Unity. Unity isn’t written in C#, but C++. Any gameplay code written still will fully or at least mostly draw from the advantages of the language that it was written in.

Any Rust game logic will be safe by default unless written in unsafe code blocks provided that the bindings are also safe and not raw bindings going directly into C++ code via C.

When I was messing around Rustlang I noticed the compiler was considerably slower than c++ compiler.
I don’t know how it is today, but there I was already thinking “-imagine this thing compiling a big project like a Unreal game, would be pain”.

If crates are more malleable now would be cool to see some experiments, but my experience those days wasn’t great.
Btw, c++ is already too slow to compile Unreal projects… Rust could be worse.

Rust compilers are slow for the same reason that C++ compilers are slow when using tons of templates. Type inference is a guided combinatorial graph search, which just takes time.
(Other “beneficiaries” of this include languages like Swift, and Haskell.)
You can write Rust that compiles faster, by making the types simpler, just like you can make C++ that compiles faster by making it look more like C. (FWIW, Unreal Engine C++ looks more like C than like modern C++, and this is probably good for the needs of the engine.)

I’m afraid I’m not well informed about the slow compile time issues, I only heard of them. I have a Ryzen 7 with 16 threads so I’m not the first who will feel this.

For what it’s worth. Rust is only 9 years old. While C/C++ are almost half a century old. C is 48, C++ is 35. Even F# is older than Rust with its 15 years. So, there is a ton of optimization that when into C/C++ compilers and run times. Rust can only get better at this point.

And depending how you use C++, the compile times also can get worse because of how modern languages aim to do more during their compile times in general these days.

I noticed it first with F# that the designers talked about wanting a very static and strong typed language because of the compile time advantages that they can gain from it. Like less bug prone code that shows its self only during run time. That’s the reason why there is no Null type a.k.a. the Billion Dollar mistake in Rust/F# for starters.

Only 9 years and it’s already rusty.

Actually, there’s also a lot of things cannot be optimized/changed in the language to keep it compatible with the past code. Actually I feel there are a lot of optimizations in the compiler to make a program run faster or safer, not necessarily faster compilation times. But this irrelevant to me, with modern CPU it’s possible to compile UE4 (nearly 3 million lines of code) in 15 minutes or less. What was most annoying: UHT running on a single thread and often taking a minute to complete its task. Thank God, it’s fixed :slight_smile:

C++ 20 modules are more interesting to me than compilation times :wink:

I only referred to the most frequently mentioned problems:

What are these problems arising from? Because you have a multitude of interconnected classes and it’s simply hard to remember who owns what and how long they exists. Raw pointers dont provide any information about ownership, even worse if the ownership is blurred, because it is managed by GC “from the back seat”.
Seg faults/dangling pointers/mem leaks are not a problem if you use smart pointers, because you clearly see which resource is shared/unique or can be null(weak).

I am not against Rust at all, but I do not see any positives of introducing it, if there is still a mess “under the hood” and any work going beyond the “mainstream” framework forces me to mentally go back to the '90s.

How many pointers does C++ even have now? There is a normal/raw, shared, auto, weak, and smart pointer? Why is it that for anything you do in C++ you need to pull a 1000 pages sized book out of a shelve? I’m afraid that’s the part where C++ isn’t only toughed badly, but is just badly designed in its own very nature.

Rust got only one pointer and it’s a safe language. C++ still isn’t exactly a “safe” language. Yeah, I know, you can also make a game with assembly language. And you can make it “safe”, too. And you maybe don’t need a 1000 pages sized book, but 1000 years instead. Still I’m not going to battle with a dull sword unless I have no other choice. Chop, chop, now. Not later.

So, C++ ain’t a productivity king either because of this. While Rust is as fast as C++ while being virtually as productive as C#. Though, for the record, Rust always feels at least a half a notch more low level than C# since Rust always uses pointers and never a GC. You don’t do dereferencing in C# really.

Well, bottom line, if C++ won’t get any simpler for doing the same, or actually even less, than at some point when MS and other companies created enough jobs, C++ may get its death sentence. Because schools won’t put up with a language that’s hard/confusing to teach either, nor new programmers.

Although it’s only 9 years old. Rust is having an effect on other languages. Like its editions system. That said, I think C++ got modules now only because of Rust. :smiley:

You get used to the name. But I would have called it Mozilla. Yes, Rust is made by Mozilla.

Now imagine a terrified Japanese uttering(maybe Ken Watanabe from the 2014 Godzilla movie): To restore balance, Mozilla must face Cong++. Let them fight!

IMHO it would be a more productive conversation if you would exaggerate so much. You don’t a 1000 pages book to write a game with Unreal C++.
There’s a raw pointer in C++ and Unreal brings its smart pointer - in 4 flavors, all quite similar in usage. Usually, weak pointer needed in the gameplay logic. Using it’s straightforward.

Rust is quite an interesting language and I would try at home just for sake trying something new if time would allow.

Although I keep hearing from other programmers that Rust is more focused on safety than performance, ensuring ownership of objects. It seems tailored for web-related things, but still didn’t any convincing explanation why need it so much for video games? I mean, UE4 provides what I need and it’s good enough. It could be a bit different to talk about writing a new engine from scratch and I’m curious about Rust-based engines :slight_smile:

What’s wrong with GC? I mean, it just works for me in UE4. Why I should need another language without it? Why isn’t just another fad? It’s not sarcasm, I’m honestly curious.
People would have to learn both Rust and C++ - since latter it’s not going anywhere, the engine wouldn’t be magically rewritten to Rust. It’s exactly the same issue with every language which wouldn’t be a scripting language designed to be something between Unreal C++ and blueprints.

The Rust language itself ain’t tailored really for any kind of specific domain, it’s a system language. However, it had to start somewhere, and it’s Mozilla who makes Rust, so since they are already in the web field, they started there by rewriting internals for the Fire Fox browser in Rust. Some ditched their old languages and rewrote in Rust to gain massive performance boosts and smaller memory foot prints.

And regarding performance, it’s only safe rust that may be slower than C++. You can write anytime anywhere inside safe Rust code so called unsafe blocks/functions for performance critical parts if needed. It’s very hard to pin point the performance loss. it really depends on the performance intensive code, but the worst number I saw was 15% in safe Rust.

A small price to pay for a clean, maintainable, and very stable code base that allows the programmers to be productive. Time is money these days more than ever. That’s the edge you get with Rust over C++ as Rust gives move than just the language itself. It’s the Cargo/Crate system, the documentation, and code formatting system. It’s this kind of consistency, wholeness, that make companies eventually choose Rust in addition to it safe and clean code. Plus, plus(see what I did here?), it’s simpler to teach to new employees.

Oh, and 15% compared to a 100FPS C++ code, doesn’t translate to 15FPS loss in Rust. It’s less than 15FPS. Hence a small price to pay. And safe Rust is always faster than managed code C#/F# in general.

Yeah, I know, it depends on your project. But, yeah, managed code has come a long way and got a ton of performance improvements. There are C# written game engines, Space Engineer is one of them, it’s not that bad. But heck, I got a Ryzen 7 3700x 16 threads, and the GC hiccups are just there. Only Unity games can get away with it, because it’s only the game logic that is written in C#, and Unity/UE games tend to be simple. Seems Cities Skyline had to use native code even with Unity.

Anyways, Rust isn’t meant to compete with C/C++, it is mean to replace it. However, it can compete with C# in my opinion. C#, or any managed code in general, looses FPS even with the boundary transition between managed/native alone. Epic Games pointed that out, and they are not happy about that. It’s sub optimal obviously. Yet, Rust is to my opinion only half a notch “harder” than C#. Yet virtually comes with the performance of C/C++.

MS made C#, and it’s part of VS, Eventually MS will add Rust to VS, too. That’s the perfect ground for competing. MS is already working Rust/WinRT just like it does on C#/WinRT. Except, Rust is still not Dot Net based, which may be off putting to some, but in general that’s better because even with cross platform .Net you got the closed platform smell of MS all over it. Unlike with Rust that is cross platform just like C/C++ and many of its APIs.

For game logic only, it’s more like learning Rust and C#, not C++. If UE supported Rust it still would support C++ of course. And Unity devs absolutely wouldn’t care about learning C++.

With game logic you actually can get away with not learning C/C++ much easier these days, than with say, trying to make a game engine.

Trying to make the UE community except Rust is a notch likelier than Unity devs. Because Unity already got C#. Yes, many actually want F# because C# is messy to work with, but anyways…

It depends on the type of engine, but for 3D it’s not pretty. Rust will suffer like Linux if MS doesn’t help out with APIs. But Rust/WinRT is on the way and is made by MS itself.

Don’t hold your breath on that present moment fact. There are translation tools already for some languages. There could be a translator in the future for Rust. This may largely erase C/C++'s ability to hide behind its established domain any longer to maker infinite excuses for being crap as a language itself. :smiley:

I prefer Rust over C/C++ obviously. But I wouldn’t want to be a Rust language dev. I’m constantly wondering why Rust lang devs even really try. Because although C/C++ may evolve very slowly, at some point when Rust hunts it down, and it does already, and then C/C++ would evolve faster trying to defend its domain. And it does. So, maybe it’s those translators that is Mozilla’s Atomic Breath to beat Cong++.

Adding Rust now, can start a transition to an UE version that is fully written in Rust by letting dev learning and getting used to it now.

If Rust has it role in Firefox “reboot” some time age and Firefox cut memory usage to 1/3 less than Chrome… it gains some respect now :slight_smile:

Well, 15% lost on the language itself and even just 10 fps lost? It’s huge and unacceptable.
Maybe if it would allow for some multithreading code written easier to get some performance back, then it could be interesting. With next-gen consoles better utilization of high CPU core count would be very appreciated.

Great point!
Although even a simple Unity game could suffer from bad performance, no way to manage memory. I heart that Unity being entirely rewritten to ECS it’s more about optimizing memory management with C# projects (and large chunks of the engine itself) than solving issues with OOP specifically.

In this light, Rust actually sounds tasty in comparison to C# which simply “costs” too much for its simplicity :slight_smile:

When I wrote “a small price to pay”, I actually quoted somebody. I just didn’t say so because I can’t find the source now. But if I recall it was a AAA dev testing out Rust, and it was also more engine related and nightmare grade bugs that delay projects and cost major project issues. Anyways…

Also I didn’t say game logic, so to clarify, because when I use metrics like “FPS” it sounds like a game. The scenario I was thinking of would be pure software code, no hardware acceleration. If you make an actual game engine with game logic written in Rust. You would have a ton of graphics code running on the GPU, not CPU. It would be way smaller than 10FPS loss in real world. With code that doesn’t show GC issues because of their small size, those small size codes you wouldn’t notice any FPS drop compared to C++, I think.

Disclaimer, I’m still learning Rust. Thinks can be worse, but also better. I’m not a web expert at all either, but I noticed benchmarks that show Rust competing with C++ code speed wise: https://www.techempower.com/benchmar…h&test=fortune

Notice how in the linked benchmarks above there is C++ code that can’t even got faster than Java and C#… clearly those source codes need better algorithms… having to bother less about the language short comings of C++, could give more time on algorithm speed ups.

I haven’t tried it yet, but it should be simpler. Rust is a safe language. That mans it’s also thread safe.