← All notes

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

Sources

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

Permanent ID: iil:note:440cf9ex

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.},
}

Markdown

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.

All artifact files (JSON index)

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.