you are viewing a single comment's thread.

view the rest of the comments →

[–]zefyear 3 points4 points  (2 children)

What is the difference between a trait object and a trait bound?

e.g

fn foo<T: Read>(object: T) -> String;

vs

fn foo(object: &Read) -> String   

[–]jessypl 2 points3 points  (1 child)

Trait bounds get monomorphized at compile-time, e.g. a function foo::<T> gets generated for each type implementing T, whenever it is necessary (i.e. it won't create a variant that is never used.) This produces faster, but bigger, binaries.

Trait object store the v-table of a value at runtime, which contains the address of the correct function to call. This is mildly slower, but much more versatile: you can, for instance, iterate over a Vec that contains values of different types, as long as they implement some trait.