w3resource

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.

Click me to see the solution

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.

Click me to see the solution

3. Write a Rust program to create a vector with integers 1 to 10. Print the element at index 4.

Click me to see the solution

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.

Click me to see the solution

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.

Click me to see the solution

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.

Click me to see the solution

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.

Click me to see the solution

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.

Click me to see the solution

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.

Click me to see the solution

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.

Click me to see the solution

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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

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