liveBook · Manning

Trait

A trait is a type-level construct that defines reusable behavior or members that types can implement or compose.

How 4 Manning books explain it

Idiomatic Rust

A Rust language feature that defines shared behavior through required or provided methods without requiring a common inheritance hierarchy.

How it is used
Provides shared behavior across different Rust types and supports design patterns without a common inheritance hierarchy.
How deep it goes
Works through definitions, implementations, bounds, and compiler errors; defers blanket implementations and advanced trait uses.
Examples
Tests shared geometry behavior for rectangles and squares, then diagnoses missing bounds in a generic description function.
What you can do after
Build it from scratch by defining a trait, implementing it for types, and bounding generic functions that call its methods.

Read how Idiomatic Rust explains trait →

Rust in Action: Systems programming concepts and techniques

The trait keyword is Rust's reserved keyword for declaring a trait.

How it is used
Defines shared behavior for types and constrains which types a generic function can use.
How deep it goes
Works through a trait declaration, an implementation, and an operator trait bound, but does not develop a comprehensive trait reference.
Examples
Uses a Read implementation for File, an Add-bounded function called with several types, and PartialEq-backed equality.
What you can do after
Build it from scratch: declare a trait, implement it for a type, and add a trait bound to a generic function.

Read how Rust in Action: Systems programming concepts and techniques explains trait →

Learn Rust in a Month of Lunches

A Rust trait is an abstract interface that defines capabilities or behavior a type can provide.

How it is used
Uses traits to give different Rust types shared capabilities and to specify what generic functions can require of their inputs.
How deep it goes
Works through derived and manual implementations, default and required methods, and compiler feedback; leaves some Display formatter syntax unexplained.
Examples
Uses a derived Debug implementation, a custom Display implementation for a cat, and a compiler error from printing the unit type.
What you can do after
Build it from scratch by declaring a trait and implementing its methods for a type; use required-method documentation to guide the implementation.

Read how Learn Rust in a Month of Lunches explains trait →

Rust Servers, Services, and Apps

A trait is a Rust abstraction that defines shared behavior or type constraints independently of a concrete type.

How it is used
Models shared behavior while building TCP, HTTP, static-content, and JSON server components.
How deep it goes
Demonstrates derived and hand-coded implementations on custom structs, without detailing trait internals.
Examples
Uses a TCP client-server exchange, an HTTP library, and a server for static pages and JSON data.
What you can do after
Apply traits to model protocol behavior and add derived or custom behavior to Rust data structures.

Read how Rust Servers, Services, and Apps explains trait →

Related concepts

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.