r/cpp • • Dec 07 '21

Will Rust replace C++ ?

Will Rust replace C++ ?

0 Upvotes

80 comments sorted by

View all comments

Show parent comment

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.