pub fn flatten<T, Slice>(collection: &[Slice]) -> Vec<T>Expand description
Flatten a collection of slices into a single vector, preserving the order of elements.
This function takes a slice of slices and concatenates all inner slices into a single Vec<T>.
The order of elements is preserved based on their original ordering in the input collection.
Time Complexity:
O(n), where n is the total number of elements across all inner slices.
§Arguments
collection- A slice of slices to be flattened.
§Type Parameters
T- The type of elements in the slices. Must implementClone.Slice- The type of the inner slices. Must implementAsRef<[T]>.
§Returns
Vec<T>- A vector containing all elements from the input slices, flattened into a single collection.
§Examples
use lowdash::flatten;
let nested = vec![vec![1, 2], vec![3, 4], vec![5]];
let flat = flatten(&nested);
assert_eq!(flat, vec![1, 2, 3, 4, 5]);use lowdash::flatten;
#[derive(Debug, PartialEq, Clone)]
struct Person {
name: String,
age: u32,
}
let people_groups = vec![
vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
],
vec![
Person { name: "Carol".to_string(), age: 35 },
Person { name: "Dave".to_string(), age: 40 },
],
];
let flat_people = flatten(&people_groups);
assert_eq!(
flat_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 },
]
);