﻿ Rust Vectors, Arrays, and Slices Exercises

# 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.

Click me to see the solution

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.

Click me to see the solution

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.

Click me to see the solution

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.

Click me to see the solution

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.

Click me to see the solution

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.

Click me to see the solution

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.

Click me to see the solution

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.

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.

﻿