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– Ricardo Pontual
@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.
– Luiz Felipe
Related: https://answall.com/q/279252/112052
– hkotsubo
@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?
– Ricardo Pontual
I’d say no, @Ricardopunctual, I think this one’s a little more specific.
– Luiz Felipe