Re: [TECH TOPIC] Rust for Linux

Miguel Ojeda
2021-07-07T23:22:07Z

On Thu, Jul 8, 2021 at 12:44 AM Laurent Pinchart
<laurent.pinchart@ideasonboard.com> wrote:
>
> You're comparing apples and pears though. A C++ function that is meant
> to transfer unique ownership of an object to the caller should return a
> std::unique_ptr<> on a container that stores the data. We're getting

Nope, I am not comparing apples and pears. I just showed you a trivial
way to make UB in C++20 with one of the types someone else mentioned.

You mention `std::unique_ptr` now. Same deal:

std::unique_ptr<int> f() {
return {}; // returns a `nullptr`
}

You will now reply: "oh, but you are *not* supposed to use it like
that!". But the point is: it is not about the simple examples, it is
about the complex cases where objects are alive for a long time,
passed across chains of calls, manipulated in several places, across
different threads, etc., etc. so that reasoning is non-local.

Don't get me wrong, `std::unique_ptr` is nice, and I have used it many
times in my career with good results. Still, it is far from what Rust
offers.

Another extremely typical example:

std::vector<int> v;
...
int * p = v.data(); // looks OK
...
v.push_back(42); // looks OK
...
f(p); // oh, wait...

> off-topic though, this mail thread isn't about comparing C++ and rust
> :-)

Well, if people bring up C++ as an alternative to Rust, they are
implying Rust does not offer much more than C++. Which is false,
misleading, and directly counters the Rust support proposal, thus I
feel the need to answer.

Again, don't get me wrong: while one can definitely see Rust as a
"cleaned up" C/C++ (it is, in a way); the key is that it *also* offers
major advantages using a few new research ideas that no other system
language had (even SPARK had to catch up [1]). It is not just a
sweeter syntax or a few fancy features here and there as many seem to
imply in many fora.

[1] https://blog.adacore.com/using-pointers-in-spark

Cheers,
Miguel