pub fn slice<T>(collection: &[T], start: isize, end: isize) -> Vec<T>where
T: Clone,Expand description
Returns a subset of the collection based on the provided start and end indices.
This function extracts a subset from the given collection starting at the specified start index
and ending before the end index. If the start or end indices are negative, they are offset
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.start- The starting index for the subset. Can be negative to indicate an offset from the end.end- The ending index for the subset. Can be negative to indicate an offset from the end.
§Type Parameters
T- The type of elements in the collection. Must implementClone.
§Returns
Vec<T>- A vector containing the subset of elements.
§Examples
use lowdash::slice;
let numbers = vec![1, 2, 3, 4, 5];
let result = slice(&numbers, 1, 3);
assert_eq!(result, vec![2, 3]);use lowdash::slice;
let numbers = vec![1, 2, 3, 4, 5];
let result = slice(&numbers, -3, -1);
assert_eq!(result, vec![3, 4]);use lowdash::slice;
#[derive(Debug, PartialEq, Clone)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Carol".to_string(), age: 35 },
Person { name: "Dave".to_string(), age: 40 },
];
let result = slice(&people, 1, 3);
assert_eq!(
result,
vec![
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Carol".to_string(), age: 35 },
]
);