#iterator #take #until

take-until

A take_until extension for iterators

2 unstable releases

0.2.0 Mar 16, 2023
0.1.0 Sep 10, 2019

#639 in Rust patterns

Download history 1778/week @ 2023-11-20 1650/week @ 2023-11-27 2646/week @ 2023-12-04 3478/week @ 2023-12-11 3169/week @ 2023-12-18 1730/week @ 2023-12-25 4392/week @ 2024-01-01 4158/week @ 2024-01-08 2468/week @ 2024-01-15 5525/week @ 2024-01-22 3824/week @ 2024-01-29 3519/week @ 2024-02-05 4581/week @ 2024-02-12 4781/week @ 2024-02-19 5368/week @ 2024-02-26 9920/week @ 2024-03-04

24,909 downloads per month
Used in 7 crates (3 directly)

MIT license

8KB
84 lines

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.

No runtime deps