This lecture, we will begin implementing integrators on our list.
The first one that we are going to implement is the end to ITER, and by implementing the into iterator
we will define how list will be converted into an iterator.
And this is common practice for types which describe a collection of some kind.
So let's go ahead and begin setting up the implementation for our end to enter.
The first thing that we're going to need to do is create a new struct called into editor over type T
that is going to have a list that is of type T.
So now inside of our list implementation block, we can come down to the bottom of it and we can say
pub fin into ed on self and this is going to return an end to ITER where we say into ITER on self.
So not much that we have to do there for this, but now we need to implement the iterator trait for
into ITER so we can say simple iterator for into iter on type T and now this is where all the magic
is going to happen for the actual iterator itself.
So we will say type item is equal to T, and then we will implement in our next method.
And next is going to be mutable reference to the self where we return an option on our self item.
So where we return our item here.
So in here, this is actually going to be something that's really easy for us to do.
So if you look at how we set up our struck here, we set it up a little differently than here.
So this is actually a tuple struct, if you remember that from earlier on in this course.
So in order for us to reference our tuple struck, we can the first element, which is also our only
element, we can say self dot pop or self .0. pop.
And that's all we need to do in order for us to get the value into our iterator.
So we have one iterator set up.
There's another iterator that we can set up, call it.
So let's set up our struct for that one.
And this is going to look very similar to what we just did.
So it's going to be ITER, and this time we're going to need to add a lifetime.
And then also on our PT and the ITER is going to be generic over some lifetime.
It doesn't care.
And then we can use a struct, a normal stroke in this case to hold the reference.
So we're going to need to hold a reference.
So next and we're going to say option.
And since it's going to be a reference, we need to specify the lifetime.
So that is why we have a lifetime attached to this struct.
So when you have a reference in your struct, remember you need to specify the lifetime of that reference.
And now we have our iter struct set up.
So now we do the same thing again.
We come down below our into iter and now we're going to set up our iter, which is going to again be
a reference to ourself and again we're going to return an error DT which is going to return in ITER
struct.
Where we say next self head as dx ref because we want a dx reference the box before we take our reference.
That way we can get the value inside of it.
So now we want to implement in the iterator
trait for our ITR type.
And remember we need to specify our lifetimes in all these.
So now we have that set up.
We can now begin implementing in the logic for our enter type.
So it's going to look very similar to what we already did.
So we'll say type item is equal to and again, since it is a reference, we need to include the lifetime
in this case.
And now.
What we can do here is going to be, again, very similar to what we did above.
So we'll say reference self
where we return an option to our item.
And now we say self.
Next take.
Now we want to map it on our node.
And inside of here we will say self.
Next equals node, next dot as dx ref.
And then we return.
A reference to our node element.
So a lot of this probably looks really similar to some code that we have done above, which you can
see here is equivalent to what we did here for Pop.
So a lot of this is already been pretty much done for us.
So implementing this in actually wasn't too bad for us.
So what we need to do now is add in our test cases to make sure that our iterations work as we would
like for them to.
So we need to do our test.
And so in here we will test our our integrators out.
So we just say let mute list
is equal to a new list.
And now we will push in some values
and now we will create our iterator.
So let mute itor equal to list dot editor and now we will assert equals on Twitter dot next and we expect
the value of some with a reference to three to be printed out.
Because we're take we're starting at the head and if we iterate again we expect two to be and now if
we do it one more time, we expect one.
And then lastly, we would anticipate none being printed out or being returned.
Excuse me.
So let's run cargo test and see what we have.
So it says we cannot it helps if I add the semicolon again.
And if we run this, we see that our iterator test passed successfully.
So we have created in two.
The Raiders.
We did our ED and we also did our into hitter, which are two different types that are implemented implementing
in the iterator trait.
So in the next lecture we will go over the assignment for the editor Mute iterator.