What are the differences between Traits (or Typeclasses) and Interfaces?

Asked

Viewed 56 times

4

Studying Rust, I began to make use of the so-called traits that, according to the language book:

We can use traits to define shared behavior in an abstract way.

The same chapter further quotes the following:

Note that traits are similar to a feature called interfaces in other languages. However, there are some differences.

Only for disambiguation (since the term trait is quite common in the area), I must say that the concept of Rust traits were inspired by the typeclasses by Haskell, so they seem to be two different names for something very similar.

Coming from a language like Typescript, I couldn’t help but notice that there really is a certain resemblance to the concept of interfaces, which also exist in languages like C# and Java.

  • Having said that, I would like to know, on occasion, what are the main differences between Rust traits and interfaces (such as Typescript).
  • There are advantages and/or disadvantages associated with each of them?
  • this concept of "trait" may not be quite universal, I just saw this concept of typeclass ai in Rails and other languages (I myself did not know :) ), for example in tests he describes the characteristics of the tests (by the way, the translation of traits is "characteristics"), you can see here: https://devblogs.microsoft.com/devops/part-2using-traits-with-differentent--test-frameworks-in-the-Unit-test-explorer/ or https://github.com/brendanconnolly/Xunit.Categories

  • @Ricardopunctual, I know it is a broad term, but I have elaborated the question limiting its meaning to the way it is used in Rust (a mechanism that, as I pointed out, is similar to Haskell’s typeclasses). On the question, I do not think there is so much ambiguity, so I do not know if an edition to further limit will be necessary. But I don’t think it has anything to do with what you linked - I can’t say for sure, though.

  • 1

    Related: https://answall.com/q/279252/112052

  • @Luizfelipe yes, your question is very contextualized, I only commented because I had already heard the term in another context besides tests but did not know, and I put in the comment for reference. Now the question that hkotsubo related to me seems to me to treat basically the same no?

  • I’d say no, @Ricardopunctual, I think this one’s a little more specific.

1 answer

1

Generally speaking, when we compare a trait of Rust with a interface of Java, we noticed some differences such as

  • In Rust, a trait can define methods, types and constants, but not mutable properties.

  • trait allows declaring as many methods for the object as for the class (static).

  • It is possible to implement the methods declared directly on trait, similar to an abstract class.

  • You can implement a trait for a stuct outside the module where the structure was declared.

  • It is also possible to implement a trait for the types defined by the language (i32, String, Vec)

  • You can implement the trait for structures with Generics specific, such as implementing only for Vec<i32>, and not to Vec<F64>.

  • When the language cannot infer, it is necessary to note whether the structure implements the trait with dynamic (Dyn) or static (impl) binding, but this is more a feature of the language than the trait.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.