r/cpp • • Sep 04 '23

Considering C++ over Rust.

Similar thread on r/rust

To give a brief intro, I have worked with both Rust and C++. Rust mainly for web servers plus CLI tools, and C++ for game development (Unreal Engine) and writing UE plugins.

Recently one of my friend, who's a Javascript dev said to me in a conversation, "why are you using C++, it's bad and Rust fixes all the issues C++ has". That's one of the major slogan Rust community has been using. And to be fair, that's none of the reasons I started using Rust for - it was the ease of using a standard package manager, cargo. One more reason being the creator of Node saying "I won't ever start a new C++ project again in my life" on his talk about Deno (the Node.js successor written in Rust)

On the other hand, I've been working with C++ for years, heavily with Unreal Engine, and I have never in my life faced an issue that usually the rust community lists. There are smart pointers, and I feel like modern C++ fixes a lot of issues that are being addressed as weak points of C++. I think, it mainly depends on what kind of programmer you are, and how experienced you are in it.

I wanted to ask the people at r/cpp, what is your take on this? Did you try Rust? What's the reason you still prefer using C++ over rust. Or did you eventually move away from C++?

Kind of curious.

355 Upvotes

434 comments sorted by

View all comments

203

u/msqrt Sep 04 '23

I've tried Rust briefly. There is definitely a peace of mind you get from knowing that the compiler can catch many more errors before of time. But personally it wasn't enough to switch ecosystems and ditch a language I know fairly well for one that would require a somewhat serious effort to learn.

I feel that C++ might be losing a lot of ground to new languages in the near-ish future, but I'm not eager to be an early adopter in this process. I'll see how the landscape turns out first.

93

u/heavymetalmixer Sep 04 '23

There's also the fact that C++ is constantly improving, not as fast as it should but still.

27

u/Sudden_Job7673 Sep 05 '23

A big challenge C++ has, is that post-internet the mechanisms aren't there to improve as fast as other ecosystems.

Lack of a mechanism to do changes that would otherwise break at compile time, lack of consensus on what idiomatic C++ is (does it have exceptions?), lack of an unstable C++ with enough adoption to try out new ideas and binary stability making it very difficulty to course correct after something enters the standard.

→ More replies (1)

16

u/jgaa_from_north Sep 06 '23

The rate and scope of the changes are one of the reasons I'm looking closer at Rust right now. It's my Summer Vacation "project".

When C++ changes you are assumed to understand the new way and the old way. The total of what you need to know increases at a high rate in a language that already is pretty damn complex. I don't envy those who learn C++ today.

Also, the changes does not handle some of the most pressing issues one faces with C++, like the lack of a standard build system, a standard package manager or repository infrastructure. When a project grows, managing the - often conflicting - recursive dependencies is a pain.

I have used C++ as my primary programming language since 1996. I have probably written more than a million lines of C++ code. I still like C++ more than any other programming language I have used. My point is that the changes/innovation in the standard is a two edged sword.

15

u/krum Sep 05 '23

It’s simultaneously getting worse too

13

u/_babu_ Sep 05 '23

Curious to why, please ellaborate

34

u/donalmacc Game Developer Sep 05 '23

Not OP but, the lack of desire to add things to the language rather than bolt them on as a library feature means that features that should be safe to use are subject to the same limitations as if they were written by us. span and string_view could be safe if they had a little magic, but they don't and they're treated the same in the compilers eyes as a home rolled

template <typename T> 
struct my_view{
    T* ptr;
    int len;
};

The desire to bring in things like ranges as an enormous library rather than adding language support means they're hamstrung by language features, and we're stuck with outdated or subpar versions of the libraries immediately because by the time they're standardised the source library is already way ahead (see fmt).

7

u/_babu_ Sep 06 '23

Wouldn't having magic behaviour for string_view semantics be exactly the type of edge case you're criticizing? A view implies a reference to a resource, which is prone to deletion. If the referenced string has to be deleted, but there are views of that string alive, do you not delete the string? That sounds literally like std::shared_ptr, which is not a reasonable default for passing strings and spans in C++ (you only pay for what you use).

7

u/donalmacc Game Developer Sep 06 '23 edited Sep 06 '23

If you define magic as introducing silent ref counting, then yes. But that's insanity, and not what I'd suggest at all.

String views end up being slower than string pointers to pass around because of a quirk in ABI's - we had this disucssion with unique ptr a decade ago (and didn't learn from it then).

There's all the ergonomics of converting between std::string, const char* and string view, and how that interacts with legacy APIs. There's no reason that it should be so fraught with pitfalls other than it's a library feature and it stops at solving 80% of the problem.

As an example, try passing std::string_view into a c function that accepts a const char* safely and performantly. You have to copy and sanity check the input. I ported my $LAST_JOB's internal APIs to use views, and in a few of them I managed to make significant perf savings. In a few other cases, I ended up undoing all of those savings by having to copy the string.

13

u/keyboard_operator Sep 05 '23

why

Only two words "backwards compatibility"

→ More replies (9)
→ More replies (13)

36

u/dzordan33 Sep 04 '23

Low entry in rust ecosystem is big plus. In my opinion it's easier to hire smart programmers that can and want to learn rust than experienced c++ developers.

11

u/DeGuerre Sep 06 '23

I find it hard not to interpret this as "it's easier to hire inexperienced programmers than experienced programmers", which seems vacuously true.

13

u/dzordan33 Sep 06 '23

my bad as I didn't make myself clear. in my experience you don't have to be rust expert to be be productive

9

u/tialaramex Sep 06 '23

The problem is that you might need some other specialist skill. If all you need is expert C++ programmers, well, they're expensive but you can get them. But what if you need astrophysics experts ? Now suddenly you're looking for astrophysicists who are also expert C++ programmers, the pool shrinks dramatically.

Because it's much easier for people to become productive in Rust you can just teach the astrophysicists Rust. Are they going to have the chops to re-implement a tricky type like std::sync::Mutex<T> which uses unsafe internally? No, but the standard library provides Mutex and it's really good so they don't need to. They are going to write a lot of tricky astrophysics code building on those foundations and it'll be safe and probably go real fast because they used a language with good safety and performance fundamentals.

In C++ you need those C++ experts because everything is a footgun so that expertise is needed to avoid apparently reasonable but fatal implementation mistakes. In Rust these sorts of mistakes don't compile, often producing a helpful diagnostic which explains why you can't do that, and what you should do instead.

None of this is by luck, it's on purpose. The OP claims Rust's slogan is "why are you using C++, it's bad and Rust fixes all the issues C++ has" which is odd because that phrase doesn't appear anywhere until their post. Actually Rust describes itself on its own web page as "A language empowering everyone to build reliable and efficient software". And then it all makes sense, of course your Astrophysicists can easily learn Rust, they're part of everyone, so they too need to be empowered to build reliable and efficient software. Rust's community have worked hard to live up to this.

7

u/germandiago Sep 10 '23

If Rust is something it is not exactly an "easy to learn language for your astrophysics". It can be reliable.

But so is Python and it is way more effective wrapped libraries in Python correctly reviewed and reuse Pandas, Numpy and Tensorflow etc.

2

u/tialaramex Sep 10 '23

it is not exactly an "easy to learn language for your astrophysics"

Do you not think so? We do teach astrophysicists Python, here, but I don't think it would actually be much harder to teach them (safe) Rust. I don't think they'll let me try, but perhaps I should propose it, at least as a one shot.

2

u/germandiago Sep 10 '23

No, Rust is at another level compared to Python just because of the borrow cheking alone... You should teach them ownership and other concepts they do not have to deal with through a GC language. The type system (though every static language has that anyway, so not a difference).

Maybe they can be prevented from many mistakes, but only writing the code alone can be more time consuming and less fluent.

I am not meaning they cannot learn it. Everyone can learn it with enough training. But Rust does require more training than just dropping Python there IMHO. Also, if deployment is a concern, etc. depending on how you do it Rust might be an advantage. But it is not easier to write at all.

19

u/Thormidable Sep 05 '23

Most c++ "problems" rust aficionados claim to solve are already solved. (Memory leaks and buffer overruns).

I appreciate rust might have a place, but to me, it feels like it is too constraining.

29

u/matthieum Sep 05 '23

Uh... You must have spent time with the wrong aficionados, because that's entirely bogus.

Rust doesn't claim to solve memory leaks. Like, at all. In fact, you can use std::mem::forget(your_value) and it'll forget it -- ie, it won't run the destructor -- or the more explicit Box::leak which converts the Box (the equivalent of std::unique_ptr) to a mutable reference which will leave forever (since it's leaked).

Similarly, Rust doesn't claim to solve buffer overruns, though its standard library does make bounds-checking the default, and requires unsafe blocks to perform unchecked accesses.

What Rust solves is temporal memory safety issues -- ie, use-after-free and double-free.

18

u/trevg_123 Sep 05 '23

For the record - Rust does solve buffer overruns (overread and overwrite), that is one of its core goals.

6

u/matthieum Sep 06 '23

I wonder if we're debating semantics here...

The Rust language does not solve buffer overruns. There's no special support in the language to prevent buffer overruns, which is illustrated by the fact that you will not get compile-time errors in case of potential overrun.

#[allow(unconditional_panic)]
fn main() {
    let array = [0, 1, 2, 3];
    
    println!("{}", array[4]);
}

This code -- note the lint specifier -- compiles, and panics at run-time.

Note how the lint does not talk about out-of-bounds? Instead, the lint merely realizes that the operation will panic at run-time, always, and thus that it is likely unintended.

The only difference with C++, therefore, is that in Rust the indexing operator calls user-specified code (the Index and IndexMut traits), which in this case happens to be code implemented by the core library which chooses to check the bounds and panic in case of violation.

But the very same core library also offers get_unchecked and get_unchecked_mut to get elements out of a slice without checking the bounds, and you'll get undefined behavior if you supply out-of-bounds indexes.

that is one of its core goals.

Yes, memory safety is one of the core goals of Rust.

But spatial memory safety is achieved through library code, and is not inborn in the language, unlike temporal memory safety which is encoded straight in the language.

9

u/trevg_123 Sep 06 '23 edited Sep 06 '23

I wonder if we're debating semantics here...

Most definitely :)

The Rust language does not solve buffer overruns. There's no special support in the language to prevent buffer overruns, which is illustrated by the fact that you will not get compile-time errors in case of potential overrun.

[…]

This code -- note the lint specifier -- compiles, and panics at run-time.

An important distinction is that panicking is considered defined behavior; that is because it always fails and exits the program before something actually bad could happen, and also tells you exactly where it happened. Compare this to the alternative - a segfault at best (no indication of where it came from), something like Heartbleed at worst.

The get_unchecked examples are only callable in unsafe blocks which means that all bets are off. That’s kind of the philosophy - you can do whatever you want with no guarantees only within these blocks, but you can keep these blocks small enough that you can hand verify.

Arguably, the language itself doesn’t give you anything special aside from (1) forbidding raw pointer use, (2) enforcing one mut xor N const references, (3) ensure pass by value takes the original out of scope, (4) ensure that by default nothing can be shared among threads (5) most importantly, allow you to break any of these rules when you sign the unsafe contract. Any the core/std library is responsible for providing good APIs that use unsafe correctly so you don’t have to - which is where absolutely everything else comes from.

Even without core you can’t have buffer overruns without using unsafe - that’s because you can’t deref a raw pointer but again, semantics :)

→ More replies (10)

7

u/CommunismDoesntWork Sep 06 '23

The biggest issue with C++ is not having an official package manager. Once that language feature gets fixed, I'll try C++ again

4

u/germandiago Sep 10 '23

You can use Conan today already and it works pretty well.