News for hackers. Stuff that matters.
Re:Rust Is Hard, Or: The Misery of Mainstream Programming by TazeTSchnitzel on Friday June 03, 2022 @05:01AM (#31605889)
> complex language which limits you

Rust really does not limit you. Anything possible in C or C++ can be done in Rust if you're willing to use unsafe code.

Re:Rust Is Hard, Or: The Misery of Mainstream Programming by jb1991 on Friday June 03, 2022 @06:57AM (#31606702)
You could also say the same thing about Assembly.
Re:Rust Is Hard, Or: The Misery of Mainstream Programming by anonymoushn on Friday June 03, 2022 @06:47AM (#31606630)
I'm not sure this is true. The aliasing model of unsafe Rust seems much more difficult to write correct code for than these other aliasing models.
Re:Rust Is Hard, Or: The Misery of Mainstream Programming by notriddle on Friday June 03, 2022 @09:31AM (#31607993)
Use UnsafeCell if that’s your problem.
Re:Rust Is Hard, Or: The Misery of Mainstream Programming by ncmncm on Friday June 03, 2022 @05:17AM (#31605987)
This is not the case if you are designing a library. Much of what is routinely used to capture semantics into mainstream C++ libraries is wholly impossible to express in Rust. (C is not even a participant, here.) This is not about template metaprogramming, just ordinary stuff.

Most of that difference is in things that happen at compile time, so this is not a question of Turing completeness.

Re:Rust Is Hard, Or: The Misery of Mainstream Programming by lelanthran on Friday June 03, 2022 @05:25AM (#31606056)
> Much of what is routinely used to capture semantics into mainstream C++ libraries is wholly impossible to express in Rust. (C is not even a participant, here.) This is not about template metaprogramming, just ordinary stuff.

Do you have any examples?

Re:Rust Is Hard, Or: The Misery of Mainstream Programming by bluGill on Friday June 03, 2022 @09:52AM (#31608292)
All above languages are turning complete, so if you can express it in one you can express it in another. The question isn't can you write it, the question is how hard is it to do, and how performant the code will be.

The heart of C++ is destructors: a bit of code that you can write and the compiler will ensure runs when code goes out of scope/is deleted. You can do this in C by remembering to manually call the right code when doing clean up, but it is easy to forget and thus error pron. (I think Rust has this too?)

C++ gives you the ability to do a virtual base class interface, which - as most people know - just means it writes a vtable behind the scene for you. Sometimes people write a vtable by hand in C: it is just a struct of function pointers, but the syntax to do it in C++ is a lot nicer. If you need an interface of some sort the win goes to C++ because the syntax is a lot nicer. (I'm not sure what Rust does about interfaces, I think it has something)

C++ gives you control over copying structs. In C structs are only copied member wise, if the struct has a pointer you need to keep track of both copies so you don't free it early. In C++ you write a copy function that will make a copy of the pointer. You can do this in C by remembering to call the right function when copying a struct, but the default is the wrong thing. (I'm not sure what rust has here, but at the very least the borrow checker will stop you from making a mistake)

C++ gives you move objects - a way to express that a struct is going out of scope, but only after a different one is taking over the contents. This is a variation of the previous, except that you know the original doesn't need valid data anymore and so you just copy pointers and null them in the original. C doesn't have this concept, you can get around it with use of pointers and manual copying of structs in the right places, but the code is ugly. (again, I'm not sure what rust does, if nothing else the borrow checker should allow the compiler to make some optimizations on this lines)

There are a lot more areas where C++ gives you syntax to write correct code that C does not. Rust intentionally doesn't have some of them (class inheritance has been abused often, but I still find it useful enough in a few cases that I think rust is wrong for throwing it out), and in other cases has come up with a better syntax. Overall I don't know enough about Rust to judge it, but I'll take C over C++ anyday.

Note, the above is about the advantages of C++ over C. C++ has a lot of warts that are out of scope for that discussion. I am not claiming C++ is perfect. If you are starting a new project you should seriously consider your language options - including some not mentioned here)

Re:Rust Is Hard, Or: The Misery of Mainstream Programming by adjav on Friday June 03, 2022 @01:22PM (#31611132)
I will note that Rust has all of those features you mentioned. 1) Traits 2) You have to be explicit, by implementing the Clone trait, otherwise arguments are passed by reference or moved. 3) Rust has both RAII and linear typing, which is effectively a proven-correct form of moves - you can't access something that has been moved, unlike C++.

Rust doesn't have class inheritance, no, but often combining traits and delegating to a "parent" field works. Although sometimes that can be considerably more cumbersome, yeah.

Re:Rust Is Hard, Or: The Misery of Mainstream Programming by ncmncm on Friday June 03, 2022 @05:30PM (#31613901)
I agree that the GP comment does not capture what Rust is unable to express.
Re:Rust Is Hard, Or: The Misery of Mainstream Programming by jstimpfle on Friday June 03, 2022 @01:54PM (#31611563)
You can also see it this way: The philosophy behind C++ is that the existance of a pointer implies ownership. In C, the existance of a pointer only implies liveness (normally; but really the code can decide on its own whether its safe to dereference the pointer).

With the C++ approach I've found myself overthinking the problem many times. But with the insight that pointers are just data, and memory management can be independent, it turns out that programming with raw pointers need not be hard. There is no problem passing a pointer without "move semantics" shenanigans.

Re:Rust Is Hard, Or: The Misery of Mainstream Programming by ncmncm on Friday June 03, 2022 @05:29PM (#31613882)
You are confused.

There is no sense, in C++, in which a naked pointer implies or suggests ownership. It is the opposite: the continued validity of a pointer value depends on ownership maintained elsewhere. Nowadays libraries keep track of ownership. Most commonly the library delegates that responsibility to Standard std::unique_ptr, for familiarity. Not doing that indicates something different in play.

(It is, in fact, UB even in C to copy the value of a pointer to an object that does not exist anymore. This is the case regardless whether it had pointed to or into a stack or a heap object.)

In C, there is no way to express library ownership of a pointer. So, in C there is always potential for confusion about ownership.

Re:Rust Is Hard, Or: The Misery of Mainstream Programming by jstimpfle on Friday June 03, 2022 @08:16PM (#31615485)
> You are confused.

I give you one advice: Think twice before writing this way again. You are not putting enough effort into understanding what I'm trying to say, and coming across as an arrogant prick.

https://news.ycombinator.com/item?id=31494099

Admittedly, I didn't make my point sufficiently clear, but this isn't even about being right or wrong. (And, once more, I understand what you say. Stop belittling me.)

Re:Rust Is Hard, Or: The Misery of Mainstream Programming by bluGill on Friday June 03, 2022 @03:43PM (#31612762)
You must be stuck in C++98 land. In C++11 a pointer always means live (though you may not have rights to store that pointer for later). We use smart pointers to force ownership.
Re:Rust Is Hard, Or: The Misery of Mainstream Programming by jstimpfle on Friday June 03, 2022 @08:25PM (#31615539)
I know. I'm not stuck in C++98 land. I'm talking about how problems are approached in C++ and more generally, OOP land. In large parts of the community, there seems to be this idea that (almost) each little pointer (naked or not isn't my point here) should also be a handle to control the lifetime of the object that it points at.

I'm not saying that it has to be this way, but there is a reason why something like unique_ptr (that can be quite tedious to use in my experience) has become so popular in many projects - IMO it is overused, often leading to more typing work instead of less.

If one is sufficiently used to this way of modelling, it can come as a surprise that memory management needn't be so hard, and doesn't require smart pointers or anything, if the program design (global flow of data etc.) is sufficiently clear.

Re:Rust Is Hard, Or: The Misery of Mainstream Programming by lelanthran on Friday June 03, 2022 @12:14PM (#31610344)
Thanks, that is a very detailed answer[1].

I'm somewhat confused about why it was downvoted - there is nothing there controversial at all.

[1] In my own toy language, I'm kinda leaning towards objects with destructors. Definitely without the footguns in C++, but destructors nonetheless.

Re:Rust Is Hard, Or: The Misery of Mainstream Programming by ncmncm on Friday June 03, 2022 @07:48PM (#31615254)
Possibly because it does not mention almost all of the C++ features that Rust still lacks.
Re:Rust Is Hard, Or: The Misery of Mainstream Programming by Santosh83 on Friday June 03, 2022 @12:17PM (#31610387)
> but I'll take C over C++ anyday.

Did you mean the opposite?

Re:Rust Is Hard, Or: The Misery of Mainstream Programming by bluGill on Friday June 03, 2022 @03:42PM (#31612732)
opps, yes I did.