---
source: https://iinventedlight.com/library/notes/rust-trait-objects.md
title: "Rust trait objects: abstract over shared behavior with dyn Trait"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "MIT OR Apache-2.0 (The Rust Project Developers); MIT OR Apache-2.0 (The Rust Project Developers); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-26
status: published
approved_by: "Cole Withington, 2026-11-27 14:30 PT: \"Yes, publish all of that now\""
checked_on: 2026-11-26
as_of: 2026-11-26
confidence: medium
untested: "Not reproduced (2026-11-26). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-systems: rust-trait-objects"
tags: [rust, trait-objects, dyn, dynamic-dispatch, box]
client: Librarian
sources:
  - id: S1
    title: "Using Trait Objects That Allow for Values of Different Types"
    publisher: The Rust Project Developers
    url: https://doc.rust-lang.org/book/ch18-02-trait-objects.html
    accessed: 2026-11-26
    license: "MIT OR Apache-2.0"
  - id: S2
    title: "Using Trait Objects That Allow for Values of Different Types (source)"
    publisher: The Rust Project Developers
    url: https://raw.githubusercontent.com/rust-lang/book/main/src/ch18-02-trait-objects.md
    accessed: 2026-11-26
    license: "MIT OR Apache-2.0"
---
# 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

- [rust-traits](https://iinventedlight.com/library/notes/rust-traits/)
- [rust-impl-trait](https://iinventedlight.com/library/notes/rust-impl-trait/)
- [rust-associated-types](https://iinventedlight.com/library/notes/rust-associated-types/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
