Today we’re talking about Rust traits . As you might’ve guessed from the title, we will not be talking about them positively. Woah woah, hang on, put down your pitchfork. A local maxima is still a maxima, I love traits as much as anyone.
Traits are one of the few programming language concepts beloved enough to earn multiple names. You might have heard about typeclasses in Haskell or protocols in Swift. Heck, if the teacher’s not watching, even interfaces can be considered a kind of trait. Elm famously left out traits, and it was so requested it spawned an FAQ explaining their absence.
Traits have a rich history dating back to 80s. How could such a mature and cherished feature have problems? Especially, problems substantial enough to warrant a blog post?
We have two dependencies a_crate and b_crate. (We’ll worry about naming another day.) One of them provides us a type A, and the other a trait BTrait. Our crate’s job is to frobinate things, and we’d like to frobinate As. Clearly the code above frobinates an A. At least until we try to compile it: