Skip to content

What is a trait object, exactly? #392

Open
@ksqsf

Description

@ksqsf

The current wording is pretty confusing and needs refining.

A trait object is an opaque value of another type that implements a set of traits

This suggests a trait object is a value.

For example, given a trait Trait, the following are all trait objects:

  • Trait
  • dyn Trait
  • ...

This suggests "dyn Trait" itself is a trait object.

Syntax TraitObjectType : dyn? TypeParamBounds

This suggests a trait object is a value of type "dyn Trait".

After reading this section, I still don't know which part in

// rfc1909. not exactly sure this is correct.
let reader: dyn Read = File::open(path)?;

is the trait object here. Is it reader, dyn Read, or File?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions