fill

Function fill 

Source
pub fn fill<T>(collection: &[T], 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 slice of items and an initial value, then returns a new Vec<T> containing the initial value repeated for the length of the input collection. The original collection remains unmodified.

Time Complexity:
O(n), where n is the number of elements in the collection.

§Arguments

  • collection - A slice of items indicating the size of the resulting vector.
  • initial - The value to fill the new vector with.

§Type Parameters

  • T - The type of elements in the collection. Must implement the Clone trait to allow duplication.

§Returns

  • Vec<T> - A new vector containing the initial value repeated for the length of the input collection.

§Examples

use lowdash::fill;

let numbers = vec![1, 2, 3, 4, 5];
let filled = fill(&numbers, 0);
assert_eq!(filled, vec![0, 0, 0, 0, 0]);
use lowdash::fill;

#[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 filled_people = fill(&people, 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 },
    ]
);