Re:'Interface' Considered Harmful
by kibwen on Friday January 09, 2015 @10:34AM
(#8862605)
Rust traits actually do allow you to have "default" implementations of methods, which gives implementers of that trait the option to avoid defining the method themselves.
For example, the trait for overloading the equality operators `==` and `!=` contains two methods, `eq` and `ne`, but `ne` has a default implementation that simply calls `eq` and negates its output. This means you only need to implement one method to get both operators, though you retain the option to implement both manually if you'd like.
Of course, such default implementations are necessarily limited by the fact that a lone trait can assume very little about the type it's being implemented on.
For example, the trait for overloading the equality operators `==` and `!=` contains two methods, `eq` and `ne`, but `ne` has a default implementation that simply calls `eq` and negates its output. This means you only need to implement one method to get both operators, though you retain the option to implement both manually if you'd like.
Of course, such default implementations are necessarily limited by the fact that a lone trait can assume very little about the type it's being implemented on.