Skip to content

find

The index of the first occurrence of needle in haystack.

The byte-slice counterpart of str::find: the standard library has no subslice search.

use helpers4::bytes::find;

Cargo feature bytes (enabled by default). To compile only this module:

cargo add helpers4 --no-default-features --features bytes

or in Cargo.toml:

[dependencies]
helpers4 = { version = "0.0.6", default-features = false, features = ["bytes"] }
pub fn find(haystack: &[u8], needle: &[u8]) -> Option<usize>
ParameterTypeDescription
haystack&[u8]The bytes to search in.
needle&[u8]The bytes to look for.

Option<usize> — The starting index of the first match, Some(0) for an empty needle, or None when there is no match.

use helpers4::bytes::find;

assert_eq!(find(b"hello world", b"o w"), Some(4));
assert_eq!(find(b"hello", b"xyz"), None);
assert_eq!(find(b"hello", b""), Some(0));

src/bytes/find.rs