What is the `dynamic` attribute?

The following is from a comment on another website:

[In] Rust, they [generics] are [monomorphized] by default, but there’s a dynamic attribute that you can stick on them to ensure that they are not. In the dynamic case, the compiler generates a small vtable with functions implementing all of the dispatch required by the generic.

Zig-style generics are not well-suited for most languages | Lobsters (comment)

I've not heard of a #[dynamic] attribute that changes trait dispatch, nor do I see one in the Reference. At first I figured this comment may have been referring to trait objects and their dyn keyword, but a subsequent comment seems to imply that this "dynamic attribute" can be applied (to functions?) without changing a library's API, which I don't think matches dyn. What is the "dynamic attribute"?

I guess they refer either to the fact that impl Trait can be usually changed to Box<dyn Trait> without changing almost anything else, or to the fact that Box<dyn Trait> can be usually used where impl Trait is expected.

1 Like

impl Trait is only a generic in argument position.

But they were probably comparing fn f<T: Trait>(t: T) to fn g(t: Box<dyn Trait>) roughly speaking. It's not an attribute at all and it's only possible with dyn-safe traits.

1 Like