pub fn repeat<T>(count: usize, initial: T) -> Vec<T>where
T: Clone,Expand description
Fill a collection with a specified value, returning a new vector with all elements set to the initial value.
This function takes a count and an initial value, then returns a new Vec<T> containing the initial value
repeated count times. The original value remains unmodified.
Time Complexity:
O(n), where n is the number of times to repeat the initial value.
§Arguments
count- The number of times to repeat the initial value.initial- The value to fill the new vector with.
§Type Parameters
T- The type of elements to be repeated. Must implement theClonetrait to allow duplication.
§Returns
Vec<T>- A new vector containing the initial value repeatedcounttimes.
§Examples
use lowdash::repeat;
let filled = repeat(5, 0);
assert_eq!(filled, vec![0, 0, 0, 0, 0]);use lowdash::repeat;
#[derive(Debug, PartialEq, Clone)]
struct Person {
name: String,
age: u32,
}
let filled_people = repeat(3, Person { name: "Dave".to_string(), age: 40 });
assert_eq!(
filled_people,
vec![
Person { name: "Dave".to_string(), age: 40 },
Person { name: "Dave".to_string(), age: 40 },
Person { name: "Dave".to_string(), age: 40 },
]
);