Remix.run Logo
▲ tialaramex 12 hours ago

> Also I was under impression that Rust at least has a similar goal?

Rust's choice here is slightly different, Rust wants to give all of the types features, whereas C++ wanted to specifically give the user defined classes as much functionality as possible.

So in Rust 5 and "Five" and '5' and [5, 5, 5, 5, 5] all can have the features you get in your own custom type named Five whereas in C++ they do not. In C++ they're not allowed to have methods ("member functions") because they are "just" primitive types. In Rust the operators are actually implemented for all the types they can be used on, not just for your own user defined classes and so on.

In a few cases this is "fake", for your own type to have a + operator you'd have to write an implementation of addition and the Rust stdlib provides an "implementation" for the machine integers which says nothing of importance because of course in reality that code is never executed, the Rust compiler knew how to add two integers together already. However most of them are real albeit often trivial.

Because Rust doesn't privilege a specific user defined type, all the user defined types get the features which make sense, including not only struct but also enum and union. It still surprises me that C++ never gave enum class (its improvement on the C enum user defined type) methods, of course a Rust enum has methods.