Trait object
A Rust trait object is a value accessed through a trait interface with its concrete implementing type hidden and methods dispatched dynamically.
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.
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.
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
- type erasureequivalent to · Type erasure is a technique that removes or hides concrete or generic type information from a representation or interface while preserving relevant operations or compile-time type checking.
- Boxuses · Box is a Rust owning smart pointer that provides indirection by storing a uniquely owned value in heap-allocated memory.
- polymorphismimplements · Polymorphism is a programming-language capability that lets a common interface or operation work with values of different types while allowing behavior to vary by type or implementation.
- dynamic dispatchuses · Dynamic dispatch is runtime selection of an operation implementation according to an object's dynamically determined type or runtime arguments.
- vtableuses · A vtable is a compiler- or runtime-generated table of function or method entries used to dynamically dispatch operations for polymorphic objects or types.
- downcastingused by · Downcasting is converting a value from a general type to a more specific compatible type, typically subject to runtime validation.
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.



