That’s called .enumerate(). The reason I didn’t suggest that is you need to keep track of the last iteration you saw. If you’re checking after your for loop is done, then you’ve already lost that info.
-Kevin On May 31, 2014, at 12:16 AM, raphael catolino <[email protected]> wrote: > In this case I think it would be simpler to just zip the iterator with > range(0, n) than implement a whole Counted iterator. > > On May 31, 2014 4:02 AM, "Kevin Ballard" <[email protected]> wrote: > I suspect a more generally interesting solution would be a Counted iterator > adaptor that keeps track of how many non-None values it's returned from > next(). You could use this to validate that your Take iterator returned the > expected number of values. > > pub struct Counted<T> { > iter: T, > /// Incremented by 1 every time `next()` returns a non-`None` value > pub count: uint > } > > impl<A, T: Iterator<A>> Iterator<A> for Counted<T> { > fn next(&mut self) -> Option<A> { > match self.iter.next() { > x@Some(_) => { > self.count += 1; > x > } > None => None > } > } > > fn size_hint(&self) -> (uint, Option<uint>) { > self.iter.size_hint() > } > } > > // plus various associated traits like DoubleEndedIterator > > -Kevin > > On May 30, 2014, at 9:31 AM, Andrew Poelstra <[email protected]> wrote: > > > Hi guys, > > > > > > Take is an iterator adaptor which cuts off the contained iterator after > > some number of elements, always returning None. > > > > I find that I need to detect whether I'm getting None from a Take > > iterator because I've read all of the elements I expected or because the > > underlying iterator ran dry unexpectedly. (Specifically, I'm parsing > > some data from the network and want to detect an early EOM.) > > > > > > This seems like it might be only me, so I'm posing this to the list: if > > there was a function Take::is_done(&self) -> bool, which returned whether > > or not the Take had returned as many elements as it could, would that be > > generally useful? > > > > I'm happy to submit a PR but want to check that this is appropriate for > > the standard library. > > > > > > > > Thanks > > > > Andrew > > > > > > > > -- > > Andrew Poelstra > > Mathematics Department, University of Texas at Austin > > Email: apoelstra at wpsoftware.net > > Web: http://www.wpsoftware.net/andrew > > > > "If they had taught a class on how to be the kind of citizen Dick Cheney > > worries about, I would have finished high school." --Edward Snowden > > > > _______________________________________________ > > Rust-dev mailing list > > [email protected] > > https://mail.mozilla.org/listinfo/rust-dev > > > _______________________________________________ > Rust-dev mailing list > [email protected] > https://mail.mozilla.org/listinfo/rust-dev >
_______________________________________________ Rust-dev mailing list [email protected] https://mail.mozilla.org/listinfo/rust-dev
