It‘s more maintainable, easier to program for, has less unexpected behavior, has a more ergonomic typesystem, safe concurrency, and a better ecosystem of libraries (and in some cases tools).
Also C++ can‘t evolve like Rust can because of backwards compatibility, ABI, and the committee
What are the trade offs though? What do we lose from using Rust?
It‘s more maintainable
Surely this can be a feature of the codebase itself, not just the language.
easier to program for
So what's the tradeoff here, what is lost to make it easier? What does "easier" mean?
has less unexpected behavior
How? I don't tend to have unexpected behaviour in C++ tbh
a better ecosystem of libraries
I find that hard to believe given how long C++ has been around.
Also C++ can‘t evolve like Rust can because of backwards compatibility, ABI, and the committee
Some might say that's a feature not a bug :D
I need to learn Rust to fully understand the benefits, but given nobody I know in the industry really cares or knows about it, I doubt there'll be a switch any time soon.
Maybe it'll be pushed by Unreal which might urge a switch over, but I just can't see it.
I would say the tradeoff is development speed. Rust is slower to program. You can‘t just hack things together. But I would say that after getting into the Rust Mindset I‘ve been nearly on par with my C++ speed.
Easier means you need to hold less things in your head that have to do with the language while programming. In C++ you often have to carry around some mental list of "proper usages", possible pitfalls, or possible UB. In Rust: If it‘s not unsafe code, don‘t worry, the borrow checker and type system have your back. It also has less ways of achieving the same thing. There are no quadrillion subtly different initializations. There is no "did I accidentally copy this" confusion. There is no "use after move". There are more often canonical ways of achieving something. Rust also has proper mechanisms for what is template hacks in C++, there is no template hell in Rust.
I need to spend a weekend researching this, it does sound promising.
At the moment my world is C++ or C# (for tools mostly) mostly focused on AAA dev. There’s no room for a new language on the platforms themselves, at least, not yet. As you say, time will tell.
I think the games industry will be the last to adopt Rust if it is to happen, simply by how incredibly averse the industry is to change. Major companies in the industry are still not on C++11 or use C++11 but write it like it‘s C with classes.
There are some promising developments tho. I think Unreal Engine has preliminary support for Rust now.
Yep that's often the case. It's getting better, with adoption being faster.
Every company I've worked with over the last few years has supported C++20 or at minimum C++17 without issue. It depends on the platform, how recently the newest project was started, how much legacy code exists, etc.
Your lucky, I know of AAA code bases which only just allowed support for range based for and lambdas in the last year (although they were creeping in before it was allowed by the standards).
Rust forces you to write a maintainable code base more often. You can achieve that via coding standards in C++, but I think Rust is exploring valuable new avenues that C++ didn't do because it got stuck in a groove. It's good for C++ too, because it reinvigorates the imagination to push new boundaries of what's possible in systems language design, which is good for C++.
I really can't understand the "all in on one of the two languages" approach I see so often. Rust pushes C++ to do better, and C++ pushes Rust to add useful features (e.g. const generics, aka non-type template arguments). I'm all for both languages pushing the boundaries of what's possible to enforce at compile time in order to improve systems software, and healthy competition is a prerequisite for that.
I think the best way I saw this being put is something along the lines of "Rust takes the things written in every C++ development best-practices and makes the compiler verify that you follow them".
"all in on one of the two languages"
The cxx crate already does wonders for transparent C++ Rust integration. Hopefully it continues to improve to the point where combining the two becomes a no-brainer.
Rust should be great for game development because you will avoid nearly all memory bugs and the most difficult-to-track-down bugs, up front. But practically speaking a serious game dev isn't likely to choose it over C++ today because it's at least a decade behind in engine/library tech. (Except for like, roguelike hobby games and stuff like that)
Example from the top of my head: Constructors. In Rust, just like in Standard ML or OCaml, constructors are mere functions. You define in your module a function called make (or new, or from) or several such of any signature and you are done.
In C++ you have default constructor, copy constructor, move constructor, rule of 3, rule of 5, can't signal error in any way but throwing an exception.
And if you define a Rust/OCaml-like make function, making constructor private? Now you have problems with make_unique-alike functions, because they require public constructors.
but given nobody I know in the industry really cares or knows about it, I doubt there'll be a switch any time soon.
This is what people were saying about Python in other industries not too long ago relatively speaking. I swear it's everywhere that doesn't need, ironically, C/C++.
Backwards compatibility. It's possible to interop C++ and Rust, but the process is far from seamless, and stuff like templates and macros are obviously impossible to roundtrip (without writing monomorphized wrapper functions). C++, by comparison, attempts to maintain backwards compatibility with any C++ code ever written, and even subsumes C to a large part.
However, this backwards compatibility also means that modern language features are impossible to put into the language. Also, no matter how it evolves, you will still have to do with warty legacy code.
Plenty of issues in C++ come from its evolution rarher than any objective problems.
Depending on your use case, lack of non-C-abi dynamic linking may also be a problem. For most projects it isn't.
Surely this can be a feature of the codebase itself, not just the language.
Sure, and you can write Fortran in any language. But if you are trying to keep you codebase nice and clean, Rust gives you way more support to do that.
For example, many error-prone C++ features like integer promotion and implicit conversions are absent from Rust. If you need a conversion, you can use it explicitly.
Stuff like concepts and modules obviously make C++ more maintainable, but they are still not supported properly in the compilers, while Rust had in many ways more powerful implementation of those features since the beginning.
Plenty of things which are undefined or implementation-defined behaviour in C++ are well-defined in Rust. Many things are UB or unspecified in C++ simply because originally different compilers or different architectures chose incompatible approaches, and nobody had the will to unify them in the standard.
The standard library of C++ is a sick joke, full of performance and corectness footguns, as well as poorly designed or plain missing APIs. Pretty much every big project reimplements much or all of stdlib. Rust standard library is very solid and used whenever the platform allows to use it.
Pattern matching is just awesome, a huge boon to correctness and readability, and will likely never be added to C++ (although Herb Sutter has been trying to add it for many years).
Build systems in C++ are a pain. Library management is a pain. In Rust, adding a dependency is usually as simple as adding a single line to Cargo.toml. Unless it has non-rust dependencies (e.g. wraps a C library), you don't need to do anything else.
a better ecosystem of libraries
It's true that C++ has more libraries. That will likely be true for a long time. However, the ones that exist in Rust are much easier to use (see above), more likely to be portable and work out of the box. There is also a lot of effort on ecosystem stability and compatibility, both at the language level and the culture level.
As one legendary compiler creator had said: one can create a language with almost magical ability, if one's prepared to sacrifice a significant amount of time compiling code.
Just leave it at the committee and organizations that control it like Microsoft/GCC/LLVM that refuse to make breaking changes. People want to iterate the language but the room for compromise has been diminished to near none.
I think there should be a C++ legacy, like an LTS, that gets non-breaking changes and a C++ head that gets iterated over. If languages like java can do API/ABI breaks, so can C++.
Because C++ has terribleless then ideal lifetime and ownership which burden the compiler massively and get in the way of optimizations all the time. (Lifetime and ownership isn't just about memory safety and exploits)
If you look at this, my guess is 90% of dev's (game dev's included) wouldn't know why this won't vectorize in two of those cases but will in another, and will blame the compiler not their code and then think they need to hand craft vectorization code to optimize it if it's causing perf issues.
There are many other selling points, but for me the biggest one of all is a better ownership model which often leads to better optimizations and more optimal design.
EDIT: For those who downvote, I don't hate C++ it's my go to language and use it exclusively enjoy writing code in it, also I have never written anything more then an hello world equivalent in Rust, I'm just pointing out things that entice me about Rust.
It's not just vectorization, it's all about aliasing it's EVERYWHERE.
In this example it's all about aliasing count:
With u8 is just an unsigned char which can point to any type including the count so it must assume that it could change
With u16 it's a unique which can't alias count so it will be able to vectorize
With u32 the data can point to count so it could alias and must assume that it can change at any iteration
Anything which the compiler can't tell is owned by the current scope and nothing else can reference it, then it needs to treat as potentially changing at every point in time, here is yet another example, and another more simple one
i am curious about one thing about rust in games toh. I had this issue while designing a custom AST.
maybe when making games you are ok with just having buffers of whatever primitive type to be dispatched to the gpu, and everything else is a afterthought.
but say you have a scene organized as a tree, and in the main loop you process some events that may make some of this node of the scene interact somehow, something like objects can bump into one another and move each other.
it seems to me that you can't have a tree of things here right? While you hold the mutable reference to a object, you can't use the borrowed reference to root to explore the children and figure out which other object has been bumped into.
wouldn't this force you to organize the entire scene tree in some convoluted way, probably less efficient than just visiting a tree?
it feels to me that the need of rigorous structures in rust helps with aliasing, but inhibits you from doing all the high performance trickery game engines do on a daily basis.
19
u/g9icy Sep 20 '22
Why though?
What would Rust do for us that C++ can't do?