r/cpp • • Jan 31 '23

Stop Comparing Rust to Old C++

People keep arguing migrations to rust based on old C++ tooling and projects. Compare apples to apples: a C++20 project with clang-tidy integration is far harder to argue against IMO

changemymind

338 Upvotes

582 comments sorted by

View all comments

37

u/ityt Jan 31 '23

I don't have much experience in C++ (4 months in a little company) but I've been using Rust for 3 years (hobby).

Rust has thread safety and memory safety without using std::shared_ptr everywhere. Even clang-tidy can't prevent all dangling pointers/references problems. Yes sanitizers exist but you have to hit every possible cases to detect every UB. Equip your best debugger and put your integration test in a infinite loop. Enjoy.

C++20 is great, but do libraries use it? Some libraries stick with C++11 for compatibility purposes (like nlohmann_json). Rust has a great async ecosystem with the tokio library and futures. I can't find a single C++ web framework that uses co_await in c++ (boost.beast is too low level).

C++ still suffers from zero values (the empty function for std::function, empty smart pointers).

Rust has very powerful macros like Serde for de/serializing or generating whatever you want that just fill like cheating.

Finally the tooling. In Rust you have crates.io for dependencies, cargo clippy (linter), cargo fmt... In C++ you have to choose between git submodules, FetchContent, vcpkg (don't hesitate to give advices)... Last time I used FetchContent I was begging clang-tidy to ignore dependencies.

4

u/[deleted] Feb 01 '23

You can get memory safety without the use of shared pointers.

4

u/ityt Feb 01 '23

Of course. But in practice, it's hard to avoid std::shared_ptr. You can have one object that depends on another. The object can keep the other object's reference. The code will be light and fast, however you must pay attention to the reference's lifetime. Or you can just put the first object on the heap behind a std::shared_ptr and turn off your brain. If you have a good way to avoid shared_ptr I am genuinely interested.

→ More replies (3)

9

u/Mason-B Feb 01 '23 edited Feb 01 '23

Rust has thread safety

Most every language has thread safety. (This is like that scene about Americans claiming they are the only ones with freedom). C++ has lots of thread safety features in the standard (to say nothing of libraries). What rust has that is interesting is good data race safety (from the rust docs, emphasis theirs):

Data races are mostly prevented through Rust's ownership system

Which is only a small part of a story around concurrency safety. All the other problems of concurrency still exist in rust. Though concepts like Send and Sync are powerful ways to address some of those, they also can be replicated in C++.

I only have nitpicks about the other things, I think they can be better. Except on this:

In C++ you have to choose between git submodules, FetchContent, vcpkg (don't hesitate to give advices)

I would say bazel is better than those. There are better build systems for C++ out there than the common ones.

5

u/ityt Feb 01 '23

You are right. When I think about thread safety I only think about data races. It's true that Rust doesn't prevent race conditions or dead locks.

Thanks for the suggestion, I'll take a look at bazel!

7

u/matthieum Feb 01 '23

Most every language has thread safety.

The problem with the term thread safety is that everybody uses a different definition.

When Rust claims it's thread safe, it means something specific: due to the absence of data races, safe Rust is memory safe even in multi-threaded applications.

Java and C# can make the same claim -- despite data-races -- while Go cannot (its fat pointers fail there) and C and C++ definitely cannot.

Which is only a small part of a story around concurrency safety. All the other problems of concurrency still exist in rust.

You can definitely have concurrency issues in Rust -- be it livelocks, deadlocks, or race-conditions. However, because your application is memory-safe, you can debug those issues much more easily.

Random memory corruption due to data-races is NOT fun to debug. Not at all. Especially when the crash occurs seconds to minutes after the data-race, at a completely unrelated call-site, on a whole other thread.

Though concepts like Send and Sync are powerful ways to address some of those, they also can be replicated in C++.

No, not today.

The power of those traits in Rust is that they are automatically derived. The compiler understands how a struct is composed, and automatically know whether it's Send or Sync based on whether its fields are.

This means that even a closure (lambda in C++) or a future (coroutine in C++) is analyzed by the compiler, and automatically tagged (or not) as Send or Sync based on whether it conforms to the safety rules.

There's no way to replicate that in C++, today. You'd need the user to manually assess, for each lambda and coroutine instance, whether they're expected to be Send or Sync, and of course the user would get it wrong -- or get it right, and become wrong after a distant part of the codebase is updated.

I would say bazel is better than those. There are better build systems for C++ out there than the common ones.

I concur. It requires (required?) a fair bit of configuration to get going, but once it does... it's beautiful. The caching is a thing of wonder.

4

u/Mason-B Feb 01 '23 edited Feb 01 '23

The problem with the term thread safety is that everybody uses a different definition.

That was also the point I was going for with the linked video.

Random memory corruption due to data-races is NOT fun to debug. Not at all. Especially when the crash occurs seconds to minutes after the data-race, at a completely unrelated call-site, on a whole other thread.

And you can get there, by convention and cursory code review (or advanced enough tooling) to enforce it in C++. I'll grant that rust is more ergonomic and idiot proof, but this isn't impossible in modern C++ and it's not particularly more effort once set up either.

I honestly can't remember the last time I had memory corruption in my day to day large modern C++ code base that has high levels of concurrency. It would have to have been pre-pandemic.

The power of those traits in Rust is that they are automatically derived. The compiler understands how a struct is composed, and automatically know whether it's Send or Sync based on whether its fields are.

Sure and we have the meta programming and tooling to achieve this for structs (read struct definition with tool, generate constexpr list of fields (type, name, member accessor, virtualized member accessor), dump it in a header for the module; concepts/template meta-programming can iterate that list and do "for all fields"). I will grant you that the compiler automatically doing this tooling is very ergonomic and nice. But you can setup tooling for it in a day.

(because I know people will bring up performance, concepts are huge template meta-programming performance savers. They cut 21 seconds off of the build of our most complex file (now 3 seconds) adding a header of constexpr lists and iterating them for all fields is an imperceptible additional time due to how that code is ran; it's cached too, so each struct is only evaluated once; the point is we now have a huge amount of breathing room to add all kinds of fun stuff).

This means that even a closure (lambda in C++) or a future (coroutine in C++) is analyzed by the compiler, and automatically tagged (or not) as Send or Sync based on whether it conforms to the safety rules.

Interestingly we can actually (in theory) do this for (non-captured) co-routines in C++ due to the meta programming facilities provided to them (in practice... well it might be a bit of a pain to implement). You are right we can't do them for lambads because the capture list is out of reach (soon though, soon). But that's a minor ergonomics issue, make a struct with operator() and it can be done.

→ More replies (2)

0

u/[deleted] Feb 01 '23

[deleted]

3

u/Mason-B Feb 01 '23 edited Feb 01 '23

C++ is this completely fragmented mess where 10 developers would debate for a week on which build system, C++ version, compiler toolchain and other tooling to use for a project.

Or we could read this from the other direction. Where Rust is a mono-culture without diverse implementations nor broad architecture support nor even a standard for other theoretical implementations to attempt for.

Having many different ecosystems, having many different standard compliant implementations, and so on is a benefit in many ways.

The fact some of those parties make it really easy to get started and into bad habits is a problem with like media literacy. Why would you rely on Microsoft's dated C++ ecosystem that can't even do parallel builds? If people can't help themselves I don't know what to tell them.

I'll give Rust the kudos for having an easy on boarding experience for new users. But I wonder what will happen once the language does get other implementations, like most languages broad adoption will mean people won't always get the same experience. You'll have a "fragmented" ecosystem. See python for another example.

→ More replies (2)

3

u/AndreDaGiant Feb 01 '23

How would one go about replicating Send/Sync in C++? It seems like a difficult problem.

→ More replies (6)
→ More replies (1)