Fun with iterators

fn is_digit(b: &u8) -> bool {
    *b >= b'0' && *b <= b'9'

fn is_sign(b: &u8) -> bool {
    *b == b'-' || *b == b'+'

fn is_when_compiled(x: &&[u8]) -> bool {
    let mut binding = x.iter();
    let itr = binding.by_ref();
    if itr.take(16).all(is_digit) {
        if itr.take(1).all(is_sign) {
        } else {
    else {

I want to test if a slice of bytes matches a particular pattern. 16 digits followed by either + or - and then 4 digits. This is what I've come up with but I'm sure you experts on Rust iterators could improve this nested if statement. Thanks!

I should add a bit upper level context:

    let mut buf = [0; 41];
    // buf gets filled with something exciting
    let f =;

This can just be itr.take(16).all(is_digit) && itr.take(1).all(is_sign) && itr.take(4).all(is_digit)


I spent the afternoon reading about iterators to get that to work and I was quite proud of myself at the time :). I know what I was thinking, I've seen things like ok_or and is_some_and for the Option Enum and was over thinking something similar might exist for iterators. Thanks for clearing the fog in my brain. That works a treat.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.