locate

Function locate 

Source
pub fn locate<'a, T>(
    iter: impl IntoIterator<Item = &'a T>,
    f: impl FnMut(&&T) -> bool,
) -> Result<&'a T, NotFoundError<T>>
Expand description

Searches an iterator for an element that satisfies a given predicate and returns a reference to it.

This function is different from Iterator::find because it returns Result<&T, NotFoundError<&T>> (not Option<&T>).

ยงExamples


let numbers = &[1, 2, 3, 4, 5];

// Find the first even number
let result = locate(numbers, |&&n| n % 2 == 0);
assert_eq!(result, Ok(&2));

// Try to find a number greater than 10 (which doesn't exist in our list)
let result = locate(numbers, |&&n| n > 10);
assert_eq!(result, Err(NotFoundError::new()));