subset

Function subset 

Source
pub fn subset<T>(collection: &[T], offset: isize, length: usize) -> Vec<T>
where T: Clone,
Expand description

Returns a subset of the collection based on the provided offset and length.

This function extracts a subset from the given collection starting at the specified offset and spanning up to length elements. If the offset is negative, it counts from the end of the collection. The function ensures that the resulting subset does not exceed the bounds of the original collection.

Time Complexity: O(n), where n is the number of elements in the subset.

§Arguments

  • collection - A slice of items from which to extract the subset.
  • offset - The starting position for the subset. Can be negative to indicate an offset from the end.
  • length - The number of elements to include in the subset.

§Type Parameters

  • T - The type of elements in the collection. Must implement Clone.

§Returns

  • Vec<T> - A vector containing the subset of elements.

§Examples

use lowdash::subset;

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