Rust trait objects: abstract over shared behavior with dyn Trait
Answer: A trait object stands in for any type that implements a trait, usually as a pointer such as Box<dyn Trait>.
Why trait objects
One limitation of vectors is that they can store elements of only one type.
A trait object points to a value and to a table of that type's trait methods, looked up at runtime.
Specify some sort of pointer, then the dyn keyword, and then specifying the relevant trait.
Trait objects must use a pointer because of dynamically sized types, as a later chapter explains.
You can use a trait object in place of a generic or concrete type; the compiler still requires the trait.
The concrete type is not known at compile time, so a method is looked up at runtime (dynamic dispatch).
What this enables
We can use trait objects in place of a generic or concrete type, and each value must still implement the trait.
When we use trait objects, Rust must use dynamic dispatch, unlike the static dispatch of monomorphized generics.
See also
Claims
- id: C1 claim: You form a trait object from a pointer, the dyn keyword, and the trait name. source: S1 evidence: "then the dyn keyword, and then specifying the relevant trait."
- id: C2 claim: Vectors on their own can store elements of only one type. source: S1 evidence: "they can store elements of only one type."
- id: C3 claim: Vectors can store elements of only one type. source: S2 evidence: "they can store elements of only one type."
Sources
- [S1] Using Trait Objects That Allow for Values of Different Types. The Rust Project Developers. https://doc.rust-lang.org/book/ch18-02-trait-objects.html . License: MIT OR Apache-2.0. Accessed 2026-11-26.
- [S2] Using Trait Objects That Allow for Values of Different Types (source). The Rust Project Developers. https://raw.githubusercontent.com/rust-lang/book/main/src/ch18-02-trait-objects.md . License: MIT OR Apache-2.0. Accessed 2026-11-26.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: The Rust Project Developers, Using Trait Objects That Allow for Values of Different Types; The Rust Project Developers, Using Trait Objects That Allow for Values of Different Types (source). MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/book/main/LICENSE-MIT). Copyright the Rust Project Developers. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: The Rust Project Developers, Using Trait Objects That Allow for Values of Different Types; The Rust Project Developers, Using Trait Objects That Allow for Values of Different Types (source). MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/book/main/LICENSE-MIT). Copyright the Rust Project Developers.
Cite this
iinventedlight. 2026. "Rust trait objects: abstract over shared behavior with dyn Trait." iinventedlight Library, iil:note:440cf9ex, version of 2026-11-26. https://iinventedlight.com/library/notes/rust-trait-objects/. License: CC-BY-4.0.
@misc{iil_note_440cf9ex,
author = {{iinventedlight}},
title = {{Rust trait objects: abstract over shared behavior with dyn Trait}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-26},
url = {https://iinventedlight.com/library/notes/rust-trait-objects/},
note = {Permanent ID iil:note:440cf9ex. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- rust-trait-objects.schema.json (schema; CC-BY-4.0)
- rust-trait-objects.table.json (table; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/rust-trait-objects.md.