Chapter 11. Traits and Generics
[A] computer scientist tends to be able to deal with nonuniform structuresâcase 1, case 2, case 3âwhile a mathematician will tend to want one unifying axiom that governs an entire system.
Donald Knuth
One of the great discoveries in programming is that itâs possible to write code that operates on values of many different types, even types that havenât been invented yet. Here are two examples:
-
Vec<T>
is generic: you can create a vector of any type of value, including types defined in your program that the authors ofVec
never anticipated. -
Many things have
.write()
methods, includingFile
s andTcpStream
s. Your code can take a writer by reference, any writer, and send data to it. Your code doesnât have to care what type of writer it is. Later, if someone adds a new type of writer, your code will already support it.
Of course, this capability is hardly new with Rust. Itâs called polymorphism, and it was the hot new programming language technology of the 1970s. By now itâs effectively universal. Rust supports polymorphism with two related features: traits and generics. These concepts will be familiar to many programmers, but Rust takes a fresh approach inspired by Haskellâs typeclasses.
Traits are Rustâs take on interfaces or abstract base classes. At first, they look just like interfaces in Java or C#. The trait for writing bytes is called std::io::Write
, and its definition in the standard library starts out like this:
trait ...
Get Programming Rust, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.