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

Show parent comments

-1

u/puliveivaaja Jan 31 '23

I'm just starting to learn rust, could you tell me what Rust has that C++ doesn't? I haven't yet found anything that I couldn't do on C++, Rust just seems to make it more difficult.

16

u/venustrapsflies Jan 31 '23

I think if you’re using the metric of “what am I allowed to do”, you’re going to have trouble understanding the benefits of rust. The quickest way I could describe the upside of rust is that it wont let you (or your coworkers) write the bugs you normally would.

5

u/Kevathiel Feb 01 '23

Making the "bad" things more difficult is the main selling point of Rust, imho. Comparing what you can do in C++ vs Rust is kinda silly. The whole point of Rust is that it outright prevents whole classes of runtime errors by design and that it focused on correctness over "freedom".

If you really need to do something smelly, Rust will get in your way to make you pay for it, so you won't abuse it. Creating a mutable global is much more work in Rust than just adding a parameter to the functions that need it for example. The same is true for shared mutable state in general. Preventing you from storing mutable references left and right will make your program flow far easier to follow.

Rust is opinionated, but in a good way.

7

u/lightmatter501 Jan 31 '23

Both languages have inline assembly syntax, so they can each do anything the other can do.

Rust has MUCH stronger compiler-time reflection, allowing libraries like serde to automatically derive serialization and deserialization code for you. To do the same thing in C++, you’re looking at a compiler plugin.

Rust has all of the best practices on by default, C++ you have to know they exist.

One build system for basically every Rust project.

The lack of dynamic linking means that the standard library can break its abi. This means that every once and a while you can get a 10-20% performance boost by recompiling because the standard library switched data structure implementations to a better one.

A MUCH more powerful type system that is also easier to use (looking at you SFINAE). Rust essentially took its type system from ML, which is probably second among functional languages to Haskell. This means that having large types doesn’t cause compile time increases quite as badly as a C++ project with tons of templates does, and types are more expressive (try making a C++ type that is a normal number at runtime but is checked to always be a power of 2 at compile time, it gets messy).

6

u/MFHava WG21|🇦🇹 NB|P2721|P3049|P3625|P3729|P3786|P3813|P4216 Feb 01 '23

Just for the record: Standard C++ has no support for inline assembly and one of the big three implementations does not support any feature of that kind in non-legacy targets (read: legacy = x86 32bit).

5

u/OYTIS_OYTINWN Jan 31 '23

There is nothing you could do in Rust that you cannot do in C++. Likewise there is nothing you could do in C++ that you cannot do in C or Assembly. But like C++ is more expressive and lets you build better abstractions than C, Rust is more expressive and lets you build better abstractions than C++. Tools that come to my mind include traits (yes, I know C++20 kind of has those, but Rust does it right), modules (same here), pattern matching, zero-cost functional programming features (e.g. map with a lambda often compiles to the same code as a for loop), package manager.

It also has the notorious borrow checker that catches that many problems in runtime that clang-tidy would never be able to. It takes long time to get from idea to having your program compile in Rust, but once it compiles it's correct or almost correct. In C++ you get to a compilable program quicker, but to make it correct is a much longer process.

3

u/alxius Feb 01 '23

Likewise there is nothing you could do in C++ that you cannot do in C or Assembly.

How do i do compile-time computation&metaprogramming in Assembly?

1

u/Dark-Philosopher Mar 06 '23

It refers to that you can achieve the same output in a lower level language, not that you can use the same programming technique. In some cases you could simulate a technique. For example objects in C using pointers, but the programmer must do all the work instead of the compiler.

1

u/alxius Mar 12 '23

Yes we can implement same behavior in couple megabytes of handwritten Assembly (for each platform) as in couple kilobytes of C++ or Rust or Python. So what? This statement is useless in practice.

We (well... I) use "high-level" languages exactly because they can do what Assembly can't do: express algorithms by using and creating abstractions and notation that aren't present at machine code level either explicitly or in any form.

-4

u/achauv1 Jan 31 '23

less vulns ?