`find()` alternative for `Vec<u8>`?

Hi.

I want to read some bytes from a file and find subslice's first index, like find() of String

But I can't find find() for Vec<u8> but in String only.

Is there a function for this?

I tried the below but is slower than converting string lossy and using find()

pub fn find_subsequence<T>(haystack: &[T], needle: &[T]) -> Option<usize>
    where for<'a> &'a [T]: PartialEq
{
    haystack.windows(needle.len()).position(|window| window == needle)
}

The standard library doesn't have a non-naive way of doing out-of-the-box substring search on anything other than &str.

3 Likes

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.