r/cpp • • Dec 07 '21

Will Rust replace C++ ?

Will Rust replace C++ ?

0 Upvotes

80 comments sorted by

View all comments

11

u/eyes-are-fading-blue Dec 07 '21

Unlikely in the foreseeable future. If either C++ compiler or tooling around it (linters/static analysis) catches up with Rust compiler in terms of race condition/lifetime guarantees, then there is no chance of Rust replacing C++. Now how possible this is, I am not sure.

2

u/Full-Spectral Dec 07 '21

Not really possible. And, even if it got close, the difference between having these things caught immediately during compile vs. having to run a 10 minute analysis pass to hope to catch close to the same amount of issues is huge. It will always be a band-aid with C++.

4

u/eyes-are-fading-blue Dec 07 '21

Care to elaborate why it is impossible?

6

u/matthieum Dec 07 '21

The short version -- I'm pretty I gave the explanation not so long ago, but can't find it again -- is:

  1. Ownership is easy, Borrow-Checking is not.

  2. Borrow-Checking requires extensive lifetime annotations, all the code would have to be annotated for things to work.

  3. Then you'd realize the existing ecosystem (starting from the STL) is fundamentally incompatible with the idea of Borrow-Checking.


For an idea of incompatibility: Borrow-Checking does not support self-references:

  • The SSO optimization of libstdc++'s std::string uses a pointer to its own internal array.

  • The Observer Pattern uses this.

  • It's frequent for callbacks to encapsulate a this pointer, and then be owned by the instance this points to.

  • ...


As such... it's not so much that it's theoretically impossible, it's that it's practically impossible.

The resulting language would be close to C++, but incompatible with all existing C++ code... starting from the standard library.

1

u/eyes-are-fading-blue Dec 07 '21

STL, in its entirety, can be considered as "unsafe" in Rust.

3

u/matthieum Dec 08 '21

The problem is not so much that the STL implementation would have to be unsafe, it's that using the STL would be unsafe.

Consider the following, extremely simplistic, example:

auto x = std::find(begin, end, y);

In order to be safe, this example requires that [begin, end) be a valid range, which means:

  1. begin and end are iterators into the same container, or pointers into the same memory allocation.

  2. end is reachable from begin.

Good luck proving those properties, (1) notably essentially require dependent types (which neither C++ nor Rust feature).

Another example:

std::map<int, std::string> map{
    { 0, "Hello, eyes-are-fading-blue! Are you a fremen?" }
}};
auto x = map[0].c_str();

//  Safe
map[1] = "Fiesta!";
std::cout << x << "\n";

//  Unsafe
map[0] = "Another one bites the dust!"s;
std::cout << x << "\n";

The very guarantee of pointer-stability when inserting in a map has to be revisited.

And it holds doubly true for unordered_map, where it's conditional on the absence of re-hashing.


The effects of borrow-checking are so pervasive to for programs to conform to borrow-checking, you have to design the entire application stack with borrow-checking in mind from the ground-up.

This means throwing away the STL as we know, and adopting a new standard library, for example modeled after Rust's.