Rust Vectors, Arrays, and Slices Exercises with Solutions and Explanations
Rust Vectors, Arrays, and Slices 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 that creates a vector of integers from 1 to 10. Slice the vector to get a sub-vector containing elements from index 3 to index 7 (inclusive). Print the sub-vector.
2. Write a Rust program that creates a vector of characters containing the letters 'A' to 'Z'. Remove the first 7 elements from the vector and then slice it to get the last 10 elements. Print the resulting sub-vector.
3. Witre a Rust program that creates a vector of random integers. Sort the vector in ascending order and slice it to get a sub-vector containing the top 3 elements. Print the sub-vector.
4. Write a Rust program that creates a vector of integers containing both even and odd numbers. Filter out the even numbers from the vector and slice it to get a sub-vector containing the first 5 odd numbers. Print the sub-vector.
5. Write a Rust program that creates a vector of floating-point numbers. Map each element of the vector to its square root. Slice the resulting vector to get a sub-vector containing the elements from index 2 to index 6. Print the sub-vector.
6. Write a Rust program that creates an array of integers of size 8 and initializes it with values from 1 to 8. Slice the array to get a sub-array containing elements from index 2 to index 5 (inclusive). Print the sub-array.
7. Write a Rust program that creates an array of strings with size 6 and initializes it with days of the week. Remove the last 2 elements from the array and slice it to get a sub-array containing the first 3 days. Print the resulting sub-array.
8. Write a Rust program that creates an array of random integers of size 12. Sort the array in descending order and slice it to get a sub-array containing the top 4 elements. Print the sub-array.
9. Write a Rust program that creates an array of floating-point numbers with size 10. Filter out the numbers less than 0.5 from the array and then slice it to get a sub-array containing the first 3 filtered numbers. Print the sub-array.
10. Write a Rust program that creates an array of integers of size 9. Map each element of the array to its cube. Slice the resulting array to get a sub-array containing the elements from index 1 to index 6. Print the sub-array.
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://w3resource.com/rust/collections_and_data_structures/vectors-arrays-slices.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics