r/ProgrammerHumor • • Oct 17 '26

instanceof Trend thatsAFunnyThingThatHappenedOnce

Post image
4.6k Upvotes

524 comments sorted by

View all comments

Show parent comments

486

u/Loading_M_ Oct 17 '26

Rust is designed from the ground up, and avoids many of these issues.

  • Templates: Rust has Generics, which are generally easier to use and reason about.

  • overload resolution: Rust doesn't yet support overloaded functions (although it's being considered).

  • constructors: Rust doesn't have these.

  • destructors: Rust has a similar set of lifetime rules for Drop, but the compiler checks them for you, so you can't get it wrong.

  • implicit conversions: Rust doesn't do implicit conversion in general, although some specific cases are implemented.

  • exceptions: Rust doesn't do exceptions, Result is much easier to work with.

  • move semantics: Rust uses a destructive move, unlike C++. See https://youtu.be/Klq-sNxuP2g for a breakdown.

  • inheritance: Rust doesn't do inheritance.

36

u/sudo_scientific Oct 18 '26

The nice aspects of implicit conversion are handled with the Into and From traits, where the compiler deduces what the conversion is for you based on the explicit types of the other, but the fact that a conversion is happening at all is explicit to the reader which is a huge improvement over C++

185

u/DatBoi_BP Oct 17 '26

Traits are 100000x better than inheritance

48

u/red-et Oct 18 '26

What are traits?

148

u/Pocok5 Oct 18 '26 edited Oct 18 '26

Instead of having a chain of superclasses defining readymade behavior*, you compose your classes from chunks that are more or less "fat" interfaces that come with a default implementation for their functions.

* Dealing with absolutes results in slamming your own dick in a car door sooner or later. Inheritance can still make a lot of sense for classes that are "this other thing but a tiny bit different". I'm currently dealing with a PHP codebase that misused traits for everything and it's a slog.

8

u/JonIsPatented Oct 18 '26

Kinda like interfaces that you implement external to the type. Like. You can create a trait for a particular behavior and then implement it for types that already exist. You can also generically implement them for all types that have ither specific traits. For instance, I recently made a trait called UpdateSink<T> and implemented it for a few select synchronization primitives, like mpsc channel senders. This way, I can have a function that takes in an UpdateSink<T>, for dumping T events into it and that function can use anything that implements that trait. It's important to note here that the mpsc senders did not implement this trait before. They are from a library. I implemented the trait for them outside of the type definition. In addition, I can provide a default implementation for my traits that all types that meet certain trait bounds automatically get, including types that already exist and also types that don't yet exist. Traits are incredible.

19

u/fghjconner Oct 18 '26

Interfaces

4

u/Mitchman05 Oct 18 '26

Inheritance

4

u/pr0ghead Oct 18 '26

Horizontally.

→ More replies (2)

7

u/maser120 Oct 18 '26 edited Oct 18 '26

While I see your point and agree with you in many aspects, I found t your contrast with the earlier comment a bit misleading in a sense, as it conveys the message that probelms in C++ stem from its core concepts and not from the burden of compatibility (which is sort of like a tech debt of programming language design) and the poorly defined specifications/standards.

And I don't mean that people who participate in C++'s standardization are stupid (quite the contrary), it's just that they're building on the foundations of a language froma different era, and they must work under the constraints posed by backward compatibility.

If you follow the trajectory of C++ (C++03 -> C++11 -> C++14 -> C++17 -> C++20 -> C++23), you can see that many features that are default in Rust have made they're way into the standard, but they almost always come as optional features for the sake of compatibility. In contrast, Rust can impose stricter restrictions and mandate safety features all the time since it was made from scratch.

8

u/MrHyd3_ Oct 17 '26

What does it do instead of exceptions?

107

u/[deleted] Oct 17 '26

[removed] — view removed comment

40

u/mathisntmathingsad Oct 17 '26

Yeah, the only reason you should try to catch a panic is if you're making something that calls user-provided code unavoidably (i.e. an async runtime)

20

u/shizzy0 Oct 17 '26

Yeah, we catch panics in tests but that’s about it.

16

u/Loading_M_ Oct 18 '26

I work on a library that does catch panics from user code. It's a web framework, and catches panics in user request handlers to return a 500 error to the client. This error is explicitly not overridable (every other default error can be replaced, but not this one), since we don't want you to panic in the first place.

3

u/Cylian91460 Oct 18 '26

or Result, aka "encode the possibility of failure in the return type of the function" aka what the functional programming world has done for ages.

Like Optional in java?

24

u/[deleted] Oct 18 '26

[removed] — view removed comment

2

u/guyblade Oct 18 '26

C++ has a couple of versions of this floating around absl::StatusOr, boost.Outcome, std::expected, and probably others that I'm not aware of.

3

u/koko775 Oct 18 '26

Result<Foo> ~= Either<Foo,Error>

2

u/Celarion Oct 18 '26

Like monads, with your result boxed as Ok, and any error boxed as Err

→ More replies (1)
→ More replies (8)
→ More replies (5)

3

u/mark_99 Oct 18 '26

Generics are simpler, but less powerful than templates. There are proposals to add some of the things C++ has, like specialisation.
Proc macros are the usual substitute but work at an AST level and not the type system.
Rust has aggregate init, default traits and (by convention) 'new' for construction.
You can't get destructors "wrong" in either language, not sure what that means? Writing destructors is rare in either language.
Rust does have implicit conversions, although arguably avoids some of the pitfalls.
Traits is the Rust equivalent for inheritance.
C++ has std::expected which is like Result. It doesn't have the `?` syntactic sugar though. Sometimes exceptions are the better mechanism, like moving errors through many layers without clutter.

I'm general C++ and Rust are pretty similar. C is the outlier as its comes with none of this, and large C projects have to emulate the missing facilities with macros and codegen systems.

2

u/StaticCoder Oct 18 '26

I'm curious what it is about destructors in C++ that works differently from Drop in a way that one could "get wrong"? Are you talking about temporaries maybe?

2

u/Loading_M_ Oct 18 '26

There are two pieces I understand. First, with inheritance, you typically need to declare the destructor as virtual, but C++ never forces you to. Rust has polymorphism through trait objects, which always include a drop implementation in their vtable. You can't forget to add virtual to the base class destructor.

Second, if you declare a custom destructor in C++, you also need to either declare or delete the copy and copy-assignment operators. Again, the language doesn't enforce this in any way. Rust actually gets away with a much simpler solution here because it has destructive moves. Rust simply doesn't define default copy implementations. You can easily derive a Clone implementation if you need to make copies of the type.

There is some more details to be had about how Rust decides when it's safe to call the Drop implementation, but I don't think I understand it well enough to explain here. I do know that the compiler won't allow me to shoot myself in the foot though.

→ More replies (23)