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
![Recommendation Algorithm Flow for Create Vector with vec![]](/flowcharts/vec-macro.png)
Solution Approach
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.
