take-until

Crates.iotake-until
lib.rstake-until
version0.2.0
sourcesrc
created_at2019-09-10 18:24:56.573042
updated_at2023-03-16 20:52:42.522203
descriptionA take_until extension for iterators
homepagehttps://github.com/hdevalke/take-until
repositoryhttps://github.com/hdevalke/take-until.git
max_upload_size
id163901
size8,763
(hdevalke)

documentation

README

Take Until

Actions Status

This crate adds the take_until method as an extension for iterators.

Examples

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;

fn main() {
    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());
}

MSRV

The MSRV is 1.56.1 stable.

Commit count: 5

cargo fmt