Rust Vectors Exercises with Solutions and Explanations
Rust Vectors Exercises [10 exercises with solution and Explanation]
[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]
1. Write a Rust program to create an empty vector and add integers 1 to 10 to it. Print the vector.
2. Write a Rust program to create a vector with integers 1 to 5. Then, append integers 6 to 10 to the vector. Finally, remove the last element from the vector and print the resulting vector.
3. Write a Rust program to create a vector with integers 1 to 10. Print the element at index 4.
4. Write a Rust program to create a vector with integers 1 to 5. Iterate over the vector and print each element multiplied by 3.
5. Write a Rust program to create a vector with integers 5, 3, 9, 1, 7. Sort the vector in ascending order and print the sorted vector.
6. Write a Rust program to create a vector with integers from 1 to 10. Filter out even numbers from the vector and print the resulting vector.
7. Write a Rust program to create a vector with integers 1 to 5. Map each element of the vector to its cube and print the resulting vector.
8. Write a Rust program to create two vectors: one with integers 1 to 5 and another with integers 6 to 10. Concatenate the two vectors and print the resulting vector.
9. Write a Rust program to create a vector with integers 1 to 10. Search for the number 8 in the vector and print whether it exists or not.
10. Write a Rust program to create a vector with integers 1 to 10. Slice the vector to get a sub-vector containing elements from index 3 to index 7 (inclusive). Print the sub-vector.
Rust Code Editor:
More to Come !
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/rust/collections_and_data_structures/vectors.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics