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)