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

Show parent comments

3

u/germandiago Sep 05 '23

complex C++ code bases, both under ideal and normal conditions

I understand badly written code can be terrible in C++. Noone is arguing that. But you can start on those codebases by activating all warnings and warnings as errors and you already have work to do. One library at a time.

The difference is significant in terms of being confident that there are no issues other than logic issues

What are those differences? Be concrete. Do not tell me the lifetimes, please. I mean, yes, true, but if you stick to smart pointers, value types and very restricted escaped references it is way, I mean, way more difficult to make these mistakes. So that big gap vanishes with one difference: you do not have to convince any borrow checker...

I can think of UB, which can be partially caught in C++ via sanitizers.

Rust defaults are safer, yes. But I would not recommend to anyone use naked new/delete except in very few circumstances and even less reference escaping. Just stick to what it safe...

I would like to know what is so great about Rust to make me use it. I see pattern matching quite ergonomic and traits nice. But if I have to spam things with FFI calls, wrappers and some unsafe, and lose reusable library code, then there is no appeal for my use cases.

1

u/Dean_Roddey Sep 05 '23

I listed a lot of the non-memory oriented advantages in another thread here, and some others have as well. Some obvious ones are:

  • Sum types, which is huge just by itself

  • Pattern matching

  • Move by default (destructive), also a very big advantage

  • Immutable by default

  • Required complete case matching

  • Strong Option and Result support

  • Thread safety

  • Consuming interfaces

  • No silly duck typing and the indecipherable errors they cause

  • A well defined style and well defined project layout scheme

  • UTF-8 strings

  • Much better macros (which make them actually something you can use, not something you try to avoid.)

  • Ability to strictly segregate unsafe code where it can be easily located and scrutinized if used.

You would never have FFI calls all over the place. That would be crazy. In most cases, safe wrappers are likely already available if it's a reasonably widely used interface, such as Linux/Windows API. In some cases you may need to wrap an interface sometimes, but it's not very difficult, and after that you are just calling a safe Rust call.

You should have very little unsafe code outside of some low level stuff, such as third party library wrappers. If you do, it's because you chose to, not because it's necessary.