Chapter 12. 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 traits from the standard library that have a significant impact on the way Rust is written. 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.”
Some of these traits are built into the Rust language. For example, a type that implements the Drop trait has a special method that Rust calls automatically whenever a value is dropped. This is critical for types that own heap memory (String, Vec) or system resources (File, TcpStream). Similarly, the Deref trait allows smart pointer types to “look and feel” like references, reducing boilerplate ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access