Build Vector with push()
Write a Rust function that takes a count n and returns a vector containing the first n multiples of 10 using Vec::push() to add elements one at a time. The push() method appends an element to the end of a vector, growing its length by one.
Vec::push() is the primary way to add elements to a vector in Rust. It takes ownership of the value and appends it to the end. If the vector's capacity is exceeded, push() automatically reallocates with double the capacity, making repeated push() calls amortized O(1). This dynamic growth is a key feature of vectors compared to fixed-size arrays.
The push() method is efficient because it operates on the end of the vector where no elements need to be shifted. Unlike insert() which shifts all subsequent elements, push() simply writes to the next available position. The vector maintains a length counter that is incremented after each push.
Time complexity is O(1) amortized per push call. Space complexity is O(n) for the resulting vector. The vector's growth strategy ensures that n push operations take O(n) time total, with occasional reallocations when capacity is reached.
Edge cases include n = 0 (return empty vector), n = 1 (vector with a single element), and ensuring the multiples start from 10 (not 0) for the first element.
Example Input & Output
Zero count returns empty vector
Four multiples of 10
Push multiples: 10, 20, 30
Two elements: 10, 20
Single element: just 10
Algorithm Flow

Solution Approach
Best Answers
fn solution(n: i32) -> Vec<i32> {
let mut result = Vec::new();
for i in 1..=n {
result.push(i * 10);
}
result
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
