Rust Does Interfaces BETTER!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi folks and welcome today we'll be comparing how interface polymorphism is handled in Rust versus Java first a short primer Java is an object-oriented programming language while rust is a multi-paradigm one rust gives you the option of organizing your code by object-oriented principles if you want but it doesn't force you into this in Java interfaces act as contracts defining a set of method signatures that implementing classes must adhere to rust doesn't have classes it instead uses data structs with Associated functions or methods a trait and rust just defines behavior that a type can exhibit practically speaking they function very similarly you need to declare that a data structure or class for Java implements a defined trait or interface and implementing multiple traits slash interfaces is allowed in both cases there's one slight difference that gives rust an edge in flexibility in Rust you can declare the implementing relationship anywhere here we have a struct followed by a trade definition and an input block which holds the trait implementation for the type above these can reside in the same file or totally different ones we are not forced into a specific way of doing this let's have a look at a Java interface in Java if I want to implement this I have to declare inside the class definition that it implements an interface in this way the relationship is stored inside the class itself in Rust I'm free to choose where destruct trait definition and implementation block reside in this case the implements relationship is not tied to the struct itself moreover because of this I can also Implement a trait defined in my code base for an external type from a library which makes plugging in external components into my API trivial this is not possible in Java I can also Implement external traits for external types but this requires a special pattern which we will leave for another time too lazy to write implementations for traits rust allows deriving automatic implementations for certain types for example deriving the EQ trait makes it easy to compare structs similarly deriving the debug trait allows for an easy way to print a struct in this scenario we have to use the debug format string colon question mark in the print statement another aspect that gives rust traits more power is the associated type where we can force implementers of a trait to declare an internal type for a defined type alias here we have the iterator trait from the standard Library and here is a matching input block for a struct we use the type keyword to Define an Associated type alias and users implementing the trait will have to define a concrete type for that particular implementation in this case we have a collection of things struct which should return the thing struct when iterated over thing is the associated type of course the associated type Alias can be used in the imple block instead of the actual type name for better readability and reusability we've used the associated type as the return type of the function we can also use Associated types as Arguments for functions if we wish and yes I said function and nut method to be explained shortly another detail you may have picked up from the examples is the different kinds of signatures that can be used in the trait definitions we have this showing a method that mutates self via a reference this which has a reference to member data but doesn't mutate it and this which doesn't use the member data of destruct at all being an Associated function unlike the two signatures above which signal methods in other words for this case the implementation just depends on the type of destruct but not the data inside it in Rust it's immediately visible if trait functions have mutable or immutable access to the member data of the struct in which case we called Methods or if they have no access to member data in Java we don't get this we'd have to dig into the implementation logic in order to check this immediate extra visibility is a superpower when it comes to keeping complex systems running as we expect one other important difference under the hood is how interface dispatch works the two languages have almost diametrically opposed approaches for those unfamiliar dispatch refers to how interface calls are delegated to the specific required implementations in the upcoming examples the empty boxes represent the interface calls while the solid colored boxes represent the actual implementations that will get executed Java uses Dynamic dispatch by default meaning that it will determine at run time which implementation is necessary and then execute the corresponding method after the implementation is identified so while the program is running it has to figure out the required implementation on the spot for some specific cases Java will perform an optimization where the implementation is set at compile time similarly to what we'll see for rust soon rust's default is static method dispatch the appropriate method implementation is determined at compile time and then baked into the binary inside the executable each trade call will be replaced by the specific implementation a process called monomorphization when the program runs the calls are executed straightforwardly like normal functions or method calls in Rust the programmer can opt into Dynamic dispatch by using trait objects these are Heap allocated boxes that can hold any struct that implements a specific trait note that the trade objects are declared using the dyn keyword coming from Dynamic before the trait name because of this it's very clear what kind of dispatch is used in all instances the Java approach sacrifices some performance and explicitness for convenience while rust defaults to Performance and transparency around the type of dispatch being used this increased visibility of rust will come at the cost of needing to learn and type a bit more syntax overall I prefer rust's approach to interfaces due to the greatly increased flexibility and explicitness a bit more complex but the effort of learning it is well worth it for the superpowers we get that's it for now hope this was useful don't be afraid to leave suggestions or questions in the comments take care and see you next time [Music]
Info
Channel: Trusty Bits
Views: 28,830
Rating: undefined out of 5
Keywords: Rust, Java, interface, trait
Id: 5QFDQRkbllo
Channel Id: undefined
Length: 9min 16sec (556 seconds)
Published: Sun Aug 13 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.