I can see the difference between dyn
and (static) impl
Traits in return position, such as:
fn foo() -> Box<dyn Trait> {}
vs
fn foo() -> impl Trait {}
Where in the dyn
version I'm allowed to return different types as long they all implement the Trait, while in the impl
version I'm only allowed to return the same type (same applies if I return a reference).
But I can't see the purpose of a dyn Trait
in argument position such as:
fn foo(x: &dyn Trait) {}
vs
fn foo(x: &impl Trait) {} // syntatic sugar of `fn foo<T: Trait>(x: &T){}`
What is the difference between the two? Why would I use one or the other? And what does the dyn
version allow me to do that the static one doesn't (that I cannot do for example by relaxing the implicit Sized
restriction with ?Sized
)?