Chapter 13. Utility Traits
Science is nothing else than the search to discover unity in the wild variety of natureâor, more exactly, in the variety of our experience. Poetry, painting, the arts are the same search, in Coleridgeâs phrase, for unity in variety.
Jacob Bronowski
This chapter describes what we call Rustâs âutilityâ traits, a grab bag of various traits from the standard library that have enough of an impact on the way Rust is written that youâll need to be familiar with them in order to write idiomatic code and design public interfaces for your crates that users will judge to be properly âRustic.â They fall into three broad categories:
- Language extension traits
- Just as the operator overloading traits we covered in the previous chapter make it possible for you to use Rustâs expression operators on your own types, there are several other standard library traits that serve as Rust extension points, allowing you to integrate your own types more closely with the language. These include
Drop
,Deref
andDerefMut
, and the conversion traitsFrom
andInto
. Weâll describe those in this chapter. - Marker traits
- These are traits mostly used to bound generic type variables to express constraints you canât capture otherwise. These include
Sized
andCopy
. - Public vocabulary traits
- These donât have any magical compiler integration; you could define equivalent traits in your own code. But they serve the important goal of setting down conventional solutions for ...
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.