Defining and implementing the Iterable trait with GATs
🚨 Warning: Speculation ahead. This is the "shiny future" page that integrates various speculative features. To see how things work today, see the corresponding page on the explainer.
To express traits like Iterable
, we can make use generic associated types -- that is, associated types with generic parameters. Here is the complete Iterable
trait:
Let's walk through it piece by piece...
- We added a
'collection
parameter toItem
. This represents "the specific collection that theItem
is borrowed from" (or, if you prefer, the lifetime for which that collection is borrowed). - The same
'collection
parameter is added toIterator
, indicating the collection that the iterator borrows its items from. - In the
iter
method, the value of'collection
comes fromself
, indicating thatiter
returns anIterator
linked toself
.
Implementing the trait
Let's write an implementation of this trait. We'll implement it for the Vec<T>
type; a &Vec<T>
can be coerced into a &[T]
slice, so we can re-use the slice Iter
that we defined before (the [playground] link includes an impl of Iterable
for [T]
as well, but we'll use Vec
here because it's more convenient).
Invoking it
Now that we have the Iterable
trait, we can reference it in our "count twice" function.
and we can invoke that by writing code like count_twice(&vec![1, 2, 3, 4, 5, 6])
.
[Play with the code from this section on the Rust playground.][playground]