To add some more things, I'll show some examples where rust is kind of like an OOP (And comparing to C#, which is what I happen to have experience with):
- You can upcast to a trait object, similar to casting a
DerivedtoInterfacein C#. This must be kept under a pointer though, because we don't have variable stack size (Yet.) - You can kind of downcast if your object implements
Any. (So essentially anything that has either no references or static references) - You can implement a trait you define locally on anything, rather than the interface/abstract class needing to be implemented at the declaration site.
while in rustinterface Foo {} class Bar: Foo { //Implement Foo here }//library A: struct Bar {} //Library B: trait Foo {} impl Foo for Bar {} //Woo hoo, now Bar magically implements Foo. - There really isn't a notion of a reference type vs a value type. It is very very clear when we're passing something that is owned or something that is not. Therefore you can tell when moving an object into another function or passing it around repeatedly is fast or if it would be faster to pass a reference around while C# for example just whisks it away with the GC.
- Constructor functions aren't a language built-in. We use object construction notation where we specify all of the fields of a struct at the time of creation, meaning we cannot have half-constructed objects safely. Instead the norm is to include a
::new()function which will construct the object for you. And/Or implementDefault.