Hacker News new | ask | show | jobs
by gumby 8 days ago
Rust's type system is less powerful and less expressive than C++ (arguably in exchange for improvements in certain cases of memory safety), and adding some of that in will bringe it closer to C++ which rust people presumably wouldn't be in favor of.
1 comments

The main things that C++ has that Rust doesn't is inheritance[0] and move constructors[1]. In practice you won't notice either unless you're writing bindings to C++ code or UI toolkits. More importantly, neither feature imperils memory safety. Rust left those out for reasons of taste[2].

What actually imperils memory safety in C++ is primarily cultural, not technical. In Rust, we have a lot of both stdlib and third-party library code that is properly designed and works well. Borrowck and the unsafe keyword are the primary technical enforcement of Rust's safety requirements, both of which could be grafted onto C++, but it probably would not do nearly as well of a job in C++ as it does in Rust.

Take, for example, the C++ contracts feature. The reason why the ISO vote on C++26 failed is because the ISO spec for contracts specifically allows compilers that do not enforce them. This is absolutely, obviously stupid; but it is also fairly typical of C++ culture. C++, being derived from C, is plagued with the notion that the programmer is ultimately responsible for performance, even though optimizing compilers have been a thing for decades and it is genuinely difficult to find performance improvements just by shuffling lines of code around. That's why they would consider it acceptable to add a safety feature and allow it to be turned off.

In Rust, you can't turn off the borrow checker. No, not even in unsafe code. Unsafe lets you lie a little to the borrow checker and type system - you can construct references from raw pointers or transmute types. But you are ultimately still beholden to those rules and you'd better make sure everything you do lines up right regardless of what happens in safe Rust.

[0] Rust traits can do inheritance-like things, so bindings to object systems with inheritance (such as GObject) will use them a lot. This still, however, results in a lot of boilerplate needing to be written - at the very least, every subclassable type needs its own trait for override implementation, etc.

[1] In Rust, all types are movable at any time. This makes self-referential structs impossible to implement safely, which means linked lists are a pain and async is basically impossible to do soundly. Pin<T> implements immovable types as a special kind of smart pointer, which is good enough to make async sound (albeit at the cost of a lot of compiler magic). But in C++ all types manage their own movement: this would be the equivalent of having a Move trait that gets called every time a struct gets moved.

[2] Specifically, "composition is preferred over inheritance." Inheritance requires developers invent a taxonomy of parent types and much early C++ code was plagued with nonsensical taxonomies.

Also template programming with constexpr + concepts, and if C++26 ever makes it, static reflection, are much more usable than the way Rust macros work.
I think that there is very little to no evidence that would suggest contracts provide substantial improvement wrt safety concerns. For that reason alone I would not call the decision of contracts being an opt-in feature obviously stupid because it is obviously not so obvious. With each feature that comes in, and with each different codebase, there's always a tradeoff to be made based on the judgment for given case and not due to general public wishful thinking. It goes the same with other similarly unsubstantiated things that C++ gets grilled for. ABI break for example.
You can’t write a generic function that sums numbers without putting traits on it in rust, it is absolutely trash compared to doing it in c++
No, the traits way of doing things is superior. C++ metaprogramming is dynamically typed with type errors raised at the instantiation site, resulting in inscrutable compile errors every time you call a generic function with the wrong thing and no obvious indication of what you're missing. In C++, if a template calls another template that adds two types, you get an error in the second function - which you might not even be aware of existing - that you need +. In Rust, if a function needs to add two types, it has to ask for Add, and that requirement bubbles up the call chain.

Granted, I have personally worked with Rust generics that also knew how to throw inscrutable compile errors (looking at you, gtk-rs) but at least in that case it was still telling me that some type I'd provided was missing a trait and not that some arbitrary substitution failed three levels deep.

I am giving you an example where it obviously isn’t working. I appreciate it but the explanation doesn’t matter for me tbh.

It just doesn’t work for me in rust but the same thing works in zig and c++.

> You can’t write a generic function that sums numbers without putting traits on it in rust

Why would you want to?

I end up needing things like this:

https://github.com/ozgrakkurt/zxbs/blob/2b0f04ad525266ae2ad0...

I had more annoying cases with integers before.