Finding exact order of hex bytes in u8 vector

Trying to figure out the Rust idiomatic way of doing the following.

Given an arbitrary array of hex bytes, find the exact order in a given another arbitrary hex array. Below is what I'm trying to do but the closure with "matches!" is not recognizing the "find_this" variable passed to the function.

Obviously not understanding something. Thanks

pub fn found_hex(
                    value: &Vec<u8>,
                    find_this: &Vec<u8>
                ) -> std::io::Result<bool> 
{
    if value.windows(find_this.len()).any(|s| matches!(s, find_this)) {
        Ok(true)
    } else {
        Ok(false)
    }
}

With the bstr crate, you can

use bstr::ByteSlice;

pub fn found_hex(value: &[u8], find_this: &[u8]) -> bool {
    value.find(find_this).is_some()
}

(playground)

bstr::ByteSlice::find documentation link

2 Likes

Worked first time, thank you!

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.