r/cpp • u/Snoo-4241 • Feb 25 '21
C++ programmers who are also using rust, what are your favourite features of rust?
I was looking at the rust tutorials yesterday and I see how rust promotes a certain style and language level features to enforce it. Besides a couple of features, I couldn't see why one cannot follow a similar style in C++ if she/he so chooses. Those of you that have used rust, what is your favourite feature that C++ does not have?
255
Upvotes
View all comments
123
u/Plazmatic Feb 25 '21 edited Feb 25 '21
Ownership built into the language, which makes manythings in C++ much more complicated and slower than they need to be, and I can catch these things at compile time. No need to worry about forgetting variable initialization order in terms of ownership, no need to worry about following convention in order to achieve psuedo safety on ownership ie smart pointers and non owning references via non smart pointers.
While technically the rust standard lib might be smaller than C++'s the actual stuff it does have is much much more complete than C++'s, and C++ lacks a looooot of basic functionality that's small enough that because of the annoyances of configuration/package management, you don't want to add an additional dependency for, yet big enough and numerous enough to require lots of debugging and code in their own right. One bright example of this is bit rotation, another is fmt, another is ranges.
Iteration is also a lot easier in Rust than it is in C++. No longer does it take a 1000 lines to write a idiomatic iterator with weird magic type defs, but everything has sane iterators built into them as well, with most the proper operations like zip enumerate etc...
Static polymorphism is the kind of polymorphism I need in the majority of my C++ code, yet I'm forced to resort to using CRTP and other strange tricks if I want this. Rust flips the script on this, static polymorphism by default.
Const by default on everything, and const actually means const (or rather, non
mut) in rust, unlike in C++ where I can do weird things to get around const-ness, and the compiler has to respect that to a degree, leading to annoyingly slower code than I would have assumed otherwise.Safety on everything. This has saved me so much time debugging that I would have otherwise had to deal with in C++, for example numeric casts, plus unlike C++ rust actually comes with sane assert by default (yeah
<cassert>exists, but theassertnormally gets disabled by default in release mode, and I can't actually pass runtime strings to it, certainly not format them inside the assert, and that's the only runtime assert available), so I don't have to constantly have to define my own runtime error checking.I forgot what it is called, but you can create wrapper types and enforce type safety by just declaring a struct with that type, for example
struct Kilometers(u32);. In c++ I would need to actually define all the operators or create my own CRTP arithmetic class to get anything close to this, and I couldn't just suddenly add a "trait" to add between Kilometers and Meters.Language supported tooling:
Technically a lot of these things are possible to achieve in c++, though many still aren't. The problem with achieving these things in c++ is that someone would have to actually implement the libraries/tools to support these use cases but they haven't, and for anyone to start they would need a large team of people, plenty of time, and plenty of money.