January 31, 2021

Traits: Rust’s unifying concept

Posted in Software at 06:49 by graham

Rust’s traits, which come from Haskell’s type classes, are a single concept that unifies all of this:

This is amazing!

Every other language I’ve used [1] has had some subset of the list above. I don’t think any of them have had them all, and they have been distinct concepts with their own keywords and syntax. Being able to unify all of that is frankly wonderful.

Rust doesn’t have a special syntax for interfaces, because traits cover that. Rust doesn’t have a special syntax for operator overloading, because traits cover that. And so on.

To underline how flexible this idea is, here’s how to do everything on the list above with just traits:

Interfaces

Logger is an interface with two methods. It looks quite similar to other languages, except it says trait instead of interface.

Simple enough.

Abstract classes

An abstract class is something you can’t instantiate directly, you have to extend it to fill in the missing methods. In Rust that looks like a trait with default implementations. Continuing the example above:

Logger now provides a default implementation of err, so StdoutLogger doesn’t have to. StdoutLogger could still provide it’s own implementation, which would override the trait default.

Mix-ins

A mix-in is behavior you add to an existing type. In our example above we defined both the trait and the struct. You can of course implement a trait from a different package. Here we implement the standard library’s Display trait, which is how Rust converts your type to a user-friendly string (like Go’s String() or Java’s toString()).

But you can do it the other way, which was quite surprising to me at first. You can implement traits you define on types from other packages, thereby adding behavior to them (“mixing-in” new behavior). That behavior is only visible to packages that import your implementation.

You have to own either the trait or the struct to implement one for the other. You can’t implement a trait you don’t own for a type you don’t own.

Operator overloading

Rust maps all of it’s operators (+, %, etc) to a trait in the ops crate. If your type implements that trait, you can use that operator.

Still just traits. Nothing extra.

Constraints on generics

This is what C++ calls Concepts, Java calls “bounded type parameters”, and the Go generics proposal calls “type constraints”. If you can define any interface as a trait, and you can also define any operator as a trait, you now have everything you need to constrain a generic type parameter.

Here’s a Nameable trait for things that can have their name set. We restrict it to being called with types that can be converted to a string (that implement the ToString trait).


trait Nameable<T: std::string::ToString> {
    fn set_name(&mut self, T);
}

Thanks to traits, this is straightforward. This is not usually the case! Constraining generics is normally a hard problem.

C++ got generics (the STL) in the mid-90's. It almost got Concepts in C++11, got very close again in C++17, and finally merged them in C++20, fifteen years later.

Go has been trying to add generics for a decade. It struggled through the years to constrain the generics with several forms of contract, before deciding to side-step the problem. In the most recent proposal instead of a general constraint you can provide a list of concrete types the generic type could be.

Behavioral markers / Attributes

Rust defines some empty marker traits to inform the compiler about the type. This feels similar to what the C / C++ / C# call attributes.

This is somewhat complicated by Rust also having actual attributes. To be fair, there are few things Rust doesn't have. It is a large language.

An example of a behavioral marker is the Copy trait. It says that your type can be duplicated by copying it's bits. You would implement this (in practice you "derive" it because it has no methods) for a type that holds only integers, for example. A counter-example would be a type that holds memory or file references; that would not be Copy.

Bonus: Method overloading, with generics

Rust does not officially have method overloading, for the same reason many languages don't: if the methods are different, give them different names. There are however two different ways you can do something that feels the same.

Rust has a way of resolving ambiguities if you implement multiple traits that define methods with the same names (a single trait cannot use a name more than once, even if the parameters differ). This requires a new syntax (TheTrait::method(&myObj)), so I didn't include it in my list in the introduction. It isn't "just traits" any more.

A nicer approach is to use generics. I didn't include this in the original list either because it's no longer just traits providing the functionality, it's generics + traits. Here's an example of a method (set_name) that can take either a string or an integer.

Note that in practice you would implement this just once, accept anything that implements ToStr (as in the "Constraints on generics" section above), and call to_string() on that. Examples are hard, OK.

It gets better. A method can be generic over it's return type. This is one of my favorite things in Rust. The function that gets called depends on the type of the variable you are assigning the output to. Check it out:

See how at the bottom we call l.pos() twice and it returns different things!. I love that.

The standard library makes great use of this with parse that will parse a string into a huge range of other types, including your own types if you implement FromStr. It's like atoi but ato*. I use it far more than I probably should because it makes me happy.

Conclusion

The official Rust blog has a more in-depth discussion of traits. If you got this far, that's a great article to read next.

Given all the different ideas you can subsume into traits, why would any future language not use them?


[1] I haven't used Haskell but I did use Miranda. If you're one of the four people on the Internet who know what that is (and how it relates to Haskell), message me!.

Leave a Comment

Note: Your comment will only appear on the site once I approve it manually. This can take a day or two. Thanks for taking the time to comment.