Trait TakeUntilExt

Source
pub trait TakeUntilExt<P>
where Self: Sized,
{ // Required method fn take_until(self, predicate: P) -> TakeUntil<Self, P>; }
Expand description

TakeUntilExt is an extension trait for iterators. It adds the Self::take_until method.

Required Methods§

Source

fn take_until(self, predicate: P) -> TakeUntil<Self, P>

Accepts elements until a predicate is true. Using this iterator is equivalent to an inclusive Iterator::take_while with a negated condition.

§Example
§Parsing the next base 128 varint from a byte slice.
use take_until::TakeUntilExt;

let varint = &[0b1010_1100u8, 0b0000_0010, 0b1000_0001];
let int: u32 = varint
    .iter()
    .take_until(|b| (**b & 0b1000_0000) == 0)
    .enumerate()
    .fold(0, |acc, (i, b)| {
        acc | ((*b & 0b0111_1111) as u32) << (i * 7)
     });
assert_eq!(300, int);
§Take Until vs Take While (from Standard Library)
use take_until::TakeUntilExt;

let items = [1, 2, 3, 4, -5, -6, -7, -8];
let filtered_take_while = items
    .into_iter()
    .take_while(|x| *x > 0)
    .collect::<Vec<i32>>();
let filtered_take_until = items
    .into_iter()
    .take_until(|x| *x <= 0)
    .collect::<Vec<i32>>();
assert_eq!([1, 2, 3, 4], filtered_take_while.as_slice());
assert_eq!([1, 2, 3, 4, -5], filtered_take_until.as_slice());

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<I, P> TakeUntilExt<P> for I
where I: Sized + Iterator, P: FnMut(&I::Item) -> bool,