pub fn interleave<T, Slice>(collections: &[Slice]) -> Vec<T>Expand description
Interleave multiple collections into a single vector, preserving the order of elements.
This function takes multiple slices and interleaves their elements into a single Vec<T>. It
iterates over the collections in a round-robin fashion, taking one element from each collection
per iteration. If a collection is exhausted, it is skipped in subsequent iterations.
Time Complexity:
O(n), where n is the total number of elements across all collections.
§Arguments
collections- A slice of slices to be interleaved.
§Type Parameters
T- The type of elements in the collections. Must implementClone.Slice- The type of the inner slices. Must implementAsRef<[T]>.
§Returns
Vec<T>- A vector containing the interleaved elements from the input collections.
§Examples
use lowdash::interleave;
let a = vec![1, 2, 3];
let b = vec![4, 5, 6, 7];
let c = vec![8, 9];
let result = interleave(&[&a[..], &b[..], &c[..]]);
assert_eq!(result, vec![1, 4, 8, 2, 5, 9, 3, 6, 7]);use lowdash::interleave;
#[derive(Debug, PartialEq, Clone)]
struct Person {
name: String,
age: u32,
}
let group1 = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
];
let group2 = vec![
Person { name: "Carol".to_string(), age: 35 },
];
let group3 = vec![
Person { name: "Dave".to_string(), age: 40 },
Person { name: "Eve".to_string(), age: 45 },
Person { name: "Frank".to_string(), age: 50 },
];
let interleaved = interleave(&[&group1[..], &group2[..], &group3[..]]);
assert_eq!(
interleaved,
vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Carol".to_string(), age: 35 },
Person { name: "Dave".to_string(), age: 40 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Eve".to_string(), age: 45 },
Person { name: "Frank".to_string(), age: 50 },
]
);