On 04/03/2014 13:23, Tommi wrote:
On Thu, Feb 13, 2014 at 10:05 AM, Simon Sapin <simon.sa...@exyr.org
<mailto:simon.sa...@exyr.org>> wrote:

Proposal:

0. An iterator is said to be "well-behaved" if, after its .next()
method has
returned None once, any subsequent call also returns None.


I agree with the spirit of your proposal. But I would change that first
clause above to read:

/An iterator is said to be "well-behaved" when its .next() method always
returns None if the iterator logically has no elements to iterate over./

"No elements to iterate over" is already what None is supposed to mean, so this doesn’t add anything.


And all iterators should, by convention, be well-behaved. Otherwise it's
impossible to pinpoint what exactly is the bug in the following code:

struct Digits {
     n: int
}

impl Iterator<int> for Digits {
     fn next(&mut self) -> Option<int> {
         self.n += 1;

         if self.n == 10 {
             None
         }
         else {
             Some(self.n)
         }
     }
}

I don’t see what’s unclear here. This is the canonical example of an ill-behaved iterator. This code is buggy, `self.n` should only be incremented when returning Some().

--
Simon Sapin
_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to