nth

Function nth 

Source
pub fn nth<T>(collection: &[T], nth: i64) -> Result<&T, NthError>
Expand description

Returns the nth element from the collection. Supports both positive and negative indices. Negative indices count from the end of the collection.

§Arguments

  • collection - A slice of items
  • nth - Index of the desired element (can be negative)

§Returns

  • Ok(&T) - The element at the specified index
  • Err(NthError) - If the index is out of bounds

§Examples

use lowdash::nth;

let numbers = vec![1, 2, 3, 4, 5];
let result = nth(&numbers, 2);
assert_eq!(result.unwrap(), &3);

let result = nth(&numbers, -2);
assert_eq!(result.unwrap(), &4);

let result = nth(&numbers, 10);
assert!(result.is_err());