13dcad92aSopenharmony_ci# `peeking_take_while`
23dcad92aSopenharmony_ci
33dcad92aSopenharmony_ci[![Build Status](https://travis-ci.org/fitzgen/peeking_take_while.png?branch=master)](https://travis-ci.org/fitzgen/peeking_take_while)
43dcad92aSopenharmony_ci
53dcad92aSopenharmony_ciProvides the `peeking_take_while` iterator adaptor method.
63dcad92aSopenharmony_ci
73dcad92aSopenharmony_ciThe `peeking_take_while` method is very similar to `take_while`, but behaves
83dcad92aSopenharmony_cidifferently when used with a borrowed iterator (perhaps returned by
93dcad92aSopenharmony_ci`Iterator::by_ref`).
103dcad92aSopenharmony_ci
113dcad92aSopenharmony_ci`peeking_take_while` peeks at the next item in the iterator and runs the
123dcad92aSopenharmony_cipredicate on that peeked item. This avoids consuming the first item yielded by
133dcad92aSopenharmony_cithe underlying iterator for which the predicate returns `false`. On the other
143dcad92aSopenharmony_cihand, `take_while` will consume that first item for which the predicate returns
153dcad92aSopenharmony_ci`false`, and it will be lost.
163dcad92aSopenharmony_ci
173dcad92aSopenharmony_ci```rust
183dcad92aSopenharmony_ci// Bring the `peeking_take_while` method for peekable iterators into
193dcad92aSopenharmony_ci// scope.
203dcad92aSopenharmony_ciuse peeking_take_while::PeekableExt;
213dcad92aSopenharmony_ci
223dcad92aSopenharmony_ci// Let's say we have two collections we want to iterate through: `xs` and
233dcad92aSopenharmony_ci// `ys`. We want to perform one operation on all the leading contiguous
243dcad92aSopenharmony_ci// elements that match some predicate, and a different thing with the rest of
253dcad92aSopenharmony_ci// the elements. With the `xs`, we will use the normal `take_while`. With the
263dcad92aSopenharmony_ci// `ys`, we will use `peeking_take_while`.
273dcad92aSopenharmony_ci
283dcad92aSopenharmony_cilet xs: Vec<u8> = (0..100).collect();
293dcad92aSopenharmony_cilet ys = xs.clone();
303dcad92aSopenharmony_ci
313dcad92aSopenharmony_cilet mut iter_xs = xs.into_iter();
323dcad92aSopenharmony_cilet mut iter_ys = ys.into_iter().peekable();
333dcad92aSopenharmony_ci
343dcad92aSopenharmony_ci{
353dcad92aSopenharmony_ci    // Let's do one thing with all the items that are less than 10.
363dcad92aSopenharmony_ci
373dcad92aSopenharmony_ci    let xs_less_than_ten = iter_xs.by_ref().take_while(|x| *x < 10);
383dcad92aSopenharmony_ci    for x in xs_less_than_ten {
393dcad92aSopenharmony_ci        do_things_with(x);
403dcad92aSopenharmony_ci    }
413dcad92aSopenharmony_ci
423dcad92aSopenharmony_ci    let ys_less_than_ten = iter_ys.by_ref().peeking_take_while(|y| *y < 10);
433dcad92aSopenharmony_ci    for y in ys_less_than_ten {
443dcad92aSopenharmony_ci        do_things_with(y);
453dcad92aSopenharmony_ci    }
463dcad92aSopenharmony_ci}
473dcad92aSopenharmony_ci
483dcad92aSopenharmony_ci// And now we will do some other thing with the items that are greater than
493dcad92aSopenharmony_ci// or equal to 10.
503dcad92aSopenharmony_ci
513dcad92aSopenharmony_ci// ...except, when using plain old `take_while` we lost 10!
523dcad92aSopenharmony_ciassert_eq!(iter_xs.next(), Some(11));
533dcad92aSopenharmony_ci
543dcad92aSopenharmony_ci// However, when using `peeking_take_while` we did not! Great!
553dcad92aSopenharmony_ciassert_eq!(iter_ys.next(), Some(10));
563dcad92aSopenharmony_ci```
57