liveBook · Manning

Trait object

A Rust trait object is a value accessed through a trait interface with its concrete implementing type hidden and methods dispatched dynamically.

How 4 Manning books explain it

Rust in Action: Systems programming concepts and techniques

A trait object is a dynamically sized Rust value representing a value of a type that implements a trait and accessed through a pointer for runtime polymorphism.

How it is used
Enables mixed-type collections and a single return type for errors from different operations.
How deep it goes
Explains pointer forms, ownership, dynamic dispatch, size, and runtime trade-offs through examples, without detailing dispatch internals.
Examples
Uses a mixed-type role-playing party, an HTTP GET request, and file-opening plus IPv6-parsing errors.
What you can do after
Build it from scratch: store different implementors behind one trait interface and return multiple error types through Box<dyn Error>.

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

Idiomatic Rust

A trait object is a dynamically dispatched Rust value accessed through a trait interface rather than its concrete type.

How it is used
Groups different concrete types behind one trait interface, including vectors that call shared methods through dynamic dispatch.
How deep it goes
Explains `dyn`, vtable dispatch, unsized storage, `Box` bounds, and optional downcasting to concrete types.
Examples
Shows boxed implementations in a vector and `Dog` and `Cat` speaking through `Box<dyn Animal>`.
What you can do after
Build it from scratch with `Vec<Box<dyn Trait>>` and call shared methods without treating `Deref` as inheritance.

Read how Idiomatic Rust explains trait object →

Code Like a Pro in Rust

A trait object is a dynamically dispatched value representing some concrete type that implements a specified trait.

How it is used
Uses trait objects to return async observer futures and to simplify error propagation in a CLI.
How deep it goes
Works through boxed futures, pinning, lifetimes, and `Send`; identifies vtable lookup as the dynamic-dispatch cost.
Examples
Shows an async observer returning a boxed future and a CLI returning `Box<dyn std::error::Error + Send + Sync>`.
What you can do after
Apply trait objects to return pinned async futures or propagate CLI errors, while weighing dispatch and error-handling trade-offs.

Read how Code Like a Pro in Rust explains trait object →

Learn Rust in a Month of Lunches

A trait object is a dynamically typed Rust value represented through a trait interface, allowing the concrete implementing type to be hidden.

How it is used
Handles functions that must return different concrete error types through one Error interface.
How deep it goes
Explains type erasure, trait-only access, compile-time sizing feedback, dynamic dispatch, and concrete-type recovery through downcasting.
Examples
Uses two custom errors, boxed Result values, parsing failures, and downcasting to MyError and RecvError.
What you can do after
Build it from scratch by returning heterogeneous errors as Box<dyn Error> and downcasting them when their concrete types matter.

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

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.