pub fn reverse<T>(collection: &[T]) -> Vec<T>where
T: Clone,Expand description
Reverse a collection, returning a new vector with the elements in reverse order.
This function takes a slice of items and returns a new Vec<T> containing all the elements
from the input collection in reverse order.
Time Complexity:
O(n), where n is the number of elements in the collection.
§Arguments
collection- A slice of items to be reversed.
§Type Parameters
T- The type of elements in the collection. Must implementClone.
§Returns
Vec<T>- A new vector containing all elements from the input collection in reverse order.
§Examples
use lowdash::reverse;
let numbers = vec![1, 2, 3, 4, 5];
let reversed = reverse(&numbers);
assert_eq!(reversed, vec![5, 4, 3, 2, 1]);use lowdash::reverse;
#[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 },
];
let reversed_people = reverse(&people);
assert_eq!(reversed_people, vec![
Person { name: "Carol".to_string(), age: 35 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Alice".to_string(), age: 25 },
]);