Remix.run Logo
Kranar 4 days ago

C++ lets you inherit from multiple classes as well. I don't see how this has anything to do with being able to add new methods to existing types.

tomjakubowski 4 days ago | parent | next [-]

There is an important difference: in Rust you can write a new trait, with new methods, and impl that trait for a type without having to change the existing structs/enums which comprise that type at all. You can also do this (with some restrictions, "the orphan rule") for types defined in another library.

In C++ classes, you have to be able to modify a class definition to extend it with new methods (or a new ancestor to multiply inherit from).

adastra22 4 days ago | parent | prev | next [-]

You can add traits (with their methods) to existing types, without modification.

bitwize 4 days ago | parent | prev [-]

C++ classes are types. Rust traits are applied to types.

Kranar 4 days ago | parent [-]

And what exactly do you think traits apply to types exactly?

If your answer doesn't start with an "m" and end with a "ethod", then you may need to re-read the Rust book found here:

https://doc.rust-lang.org/book/ch10-02-traits.html