Create Vector with vec![]
Write a Rust function that takes three integer values and returns a vector containing them using the vec![] macro. The vec![] macro is Rust's idiomatic way to create vectors, providing a concise syntax similar to array literals in other languages.
The vec![] macro expands to code that creates a Vec and pushes each element into it. It is more convenient than manually creating an empty Vec and calling push() for each element. The macro works with any number of elements and automatically infers the element type from the provided values.
Vectors (Vec
Time complexity is O(n) where n is the number of elements. The macro internally allocates the vector and pushes each element. Space complexity is O(n) for the resulting vector allocation.
Edge cases include all elements being the same value, negative numbers, and ensuring the function works with the i32 type.
Example Input & Output
All sevens
With negative and zero
Three elements
All same value
Three increasing
Algorithm Flow
Solution Approach
Create a Vec with specified elements using the vec! macro. The macro generates a Vec containing the given values. It infers the element type from the provided values.
vec! is the most convenient way to create a Vec with initial values. For an empty Vec use Vec::new(). For a Vec of repeated values: vec![0; 5] creates [0,0,0,0,0].
Time O(n), Space O(n).
Best Answers
fn solution(a: i32, b: i32, c: i32) -> Vec<i32> {
vec![a, b, c]
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
