Question about where bounds on traits
I want to write a trait that allows iteration over the type. If we had associated type constructors this would be easy but since we don't I tried to do this
trait Iterable
where for<'a> &'a Self: IntoIterator
{}
Unfortunately this fails as soon as I try to use T: Iterable as a bound (playground link).
Is this intended behaviour and is there a better way to express this?
Posted Jul 9, 2017, 11:30 PM UTC · edited
61
If you want the
TofFoo<T>to be constrained toIterable, you need to explicitly spell out the additional bounds for theIterabletrait again:Here's a playground link which also demonstrates implementing the
Iterabletrait and using an instance ofFooin aforloopI think the reasoning for this is explicitness: your
Tparameter doesn't have any "hidden restrictions" on your type parameter beyond what you specify in-place (although there has been some discussion in the ergonomics initiative to add "implied bounds", which AFAICT would make your original example compile).