//@ In the following, we will look into the iterator mechanism of Rust and make our `BigInt` compatible
//@ with the `for` loops. Of course, this is all about implementing certain traits again. In particular,
//@ In the following, we will look into the iterator mechanism of Rust and make our `BigInt` compatible
//@ with the `for` loops. Of course, this is all about implementing certain traits again. In particular,
//@ this trait mandates a single function `next` returning an `Option<Self::Item>`, where `Item` is an
//@ associated type chosen by the implementation. (There are many more methods provided for `Iterator`,
//@ but they all have default implementations, so we don't have to worry about them right now.)
//@ this trait mandates a single function `next` returning an `Option<Self::Item>`, where `Item` is an
//@ associated type chosen by the implementation. (There are many more methods provided for `Iterator`,
//@ but they all have default implementations, so we don't have to worry about them right now.)
//@ digit comes first. So, we have to write down some type, and implement `Iterator` for it such that `next` returns the digits
//@ one-by-one. Clearly, the iterator must somehow be able to access the number it iterates over, and it must store its current
//@ location. However, it cannot *own* the `BigInt`, because then the number would be gone after iteration! That'd certainly be bad.
//@ digit comes first. So, we have to write down some type, and implement `Iterator` for it such that `next` returns the digits
//@ one-by-one. Clearly, the iterator must somehow be able to access the number it iterates over, and it must store its current
//@ location. However, it cannot *own* the `BigInt`, because then the number would be gone after iteration! That'd certainly be bad.
//@ `Iter`, we must have an `Iter<'a>` that borrows the number for lifetime `'a`. This is our first example of
//@ a data-type that's polymorphic in a lifetime, as opposed to a type. <br/>
//@ `usize` here is the type of unsigned, pointer-sized numbers. It is typically the type of "lengths of things",
//@ `Iter`, we must have an `Iter<'a>` that borrows the number for lifetime `'a`. This is our first example of
//@ a data-type that's polymorphic in a lifetime, as opposed to a type. <br/>
//@ `usize` here is the type of unsigned, pointer-sized numbers. It is typically the type of "lengths of things",
// All we need now is a function that creates such an iterator for a given `BigInt`.
impl BigInt {
//@ Notice that when we write the type of `iter`, we don't actually have to give the lifetime parameter of `Iter`. Just as it is
// All we need now is a function that creates such an iterator for a given `BigInt`.
impl BigInt {
//@ Notice that when we write the type of `iter`, we don't actually have to give the lifetime parameter of `Iter`. Just as it is
//@ same. (See the last section of [part 06](part06.html).)
fn iter(&self) -> Iter {
Iter { num: self, idx: self.data.len() } /*@*/
//@ same. (See the last section of [part 06](part06.html).)
fn iter(&self) -> Iter {
Iter { num: self, idx: self.data.len() } /*@*/
// digits coming first. Write a testcase for it.
// ## Iterator invalidation and lifetimes
//@ You may have been surprised that we had to explicitly annotate a lifetime when we wrote `Iter`. Of
// digits coming first. Write a testcase for it.
// ## Iterator invalidation and lifetimes
//@ You may have been surprised that we had to explicitly annotate a lifetime when we wrote `Iter`. Of
-//@ course, with lifetimes being present at every borrow in Rust, this is only consistent. But do we at
+//@ course, with lifetimes being present at every reference in Rust, this is only consistent. But do we at
//@ least gain something from this extra annotation burden? (Thankfully, this burden only occurs when we
//@ define *types*, and not when we define functions - which is typically much more common.)
//@ least gain something from this extra annotation burden? (Thankfully, this burden only occurs when we
//@ define *types*, and not when we define functions - which is typically much more common.)
// ## Iterator conversion trait
//@ If you closely compare the `for` loop in `main` above, with the one in `part06::vec_min`, you will notice that we were able to write
//@ `for e in v` earlier, but now we have to call `iter`. Why is that? Well, the `for` sugar is not actually tied to `Iterator`.
// ## Iterator conversion trait
//@ If you closely compare the `for` loop in `main` above, with the one in `part06::vec_min`, you will notice that we were able to write
//@ `for e in v` earlier, but now we have to call `iter`. Why is that? Well, the `for` sugar is not actually tied to `Iterator`.
//@ That's a trait of types that provide a *conversion* function into some kind of iterator. These conversion traits are a frequent
//@ pattern in Rust: Rather than demanding that something is an iterator, or a string, or whatever; one demands that something
//@ can be converted to an iterator/string/whatever. This provides convenience similar to overloading of functions: The function
//@ That's a trait of types that provide a *conversion* function into some kind of iterator. These conversion traits are a frequent
//@ pattern in Rust: Rather than demanding that something is an iterator, or a string, or whatever; one demands that something
//@ can be converted to an iterator/string/whatever. This provides convenience similar to overloading of functions: The function
//@ of the right type, the conversion function will not do anything and trivially be optimized away.
//@ If you have a look at the documentation of `IntoIterator`, you will notice that the function `into_iter` it provides actually
//@ of the right type, the conversion function will not do anything and trivially be optimized away.
//@ If you have a look at the documentation of `IntoIterator`, you will notice that the function `into_iter` it provides actually
}
// With this in place, you can now replace `b.iter()` in `main` by `&b`. Go ahead and try it! <br/>
//@ Wait, `&b`? Why that? Well, we implemented `IntoIterator` for `&BigInt`. If we are in a place where `b` is already borrowed, we can
}
// With this in place, you can now replace `b.iter()` in `main` by `&b`. Go ahead and try it! <br/>
//@ Wait, `&b`? Why that? Well, we implemented `IntoIterator` for `&BigInt`. If we are in a place where `b` is already borrowed, we can
-//@ just do `for digit in b`. If however, we own `b`, we have to borrow it. Alternatively, we could implement `IntoIterator`
+//@ just do `for digit in b`. If however, we own `b`, we have to create a reference to it. Alternatively, we could implement `IntoIterator`
//@ for `BigInt` - which, as already mentioned, would mean that `b` is actually consumed by the iteration, and gone. This can easily happen,
//@ for example, with a `Vec`: Both `Vec` and `&Vec` (and `&mut Vec`) implement `IntoIterator`, so if you do `for e in v`, and `v` has type `Vec`,
//@ then you will obtain ownership of the elements during the iteration - and destroy the vector in the process. We actually did that in
//@ `part01::vec_min`, but we did not care. You can write `for e in &v` or `for e in v.iter()` to avoid this.
//@ for `BigInt` - which, as already mentioned, would mean that `b` is actually consumed by the iteration, and gone. This can easily happen,
//@ for example, with a `Vec`: Both `Vec` and `&Vec` (and `&mut Vec`) implement `IntoIterator`, so if you do `for e in v`, and `v` has type `Vec`,
//@ then you will obtain ownership of the elements during the iteration - and destroy the vector in the process. We actually did that in
//@ `part01::vec_min`, but we did not care. You can write `for e in &v` or `for e in v.iter()` to avoid this.