
Practice 50 Slices Coding Questions
Q1. Write a Rust program to create a slice from an array and print its elements.
Input:
Array: [1, 2, 3, 4, 5]
Expected Output:
Slice: [2, 3, 4]
Q2. Write a Rust program to get the first three elements of an array as a slice.
Input:
Array: [1, 2, 3, 4, 5]
Expected Output:
Slice: [1, 2, 3]
Q3. Write a Rust program to get a slice of the last three elements of an array.
Input:
Array: [1, 2, 3, 4, 5]
Expected Output:
Slice: [3, 4, 5]
Q4. Write a Rust program to slice an array from a given start index to an end index.
Input:
Array: [10, 20, 30, 40, 50], Start index: 1, End index: 4
Expected Output:
Slice: [20, 30, 40]
Q5. Write a Rust program to check if a given slice is empty.
Input:
Slice: []
Expected Output:
Slice is empty.
Q6. Write a Rust program to find the length of a slice.
Input:
Slice: [1, 2, 3]
Expected Output:
Length: 3
Q7. Write a Rust program to reverse the elements of a slice.
Input:
Slice: [1, 2, 3, 4]
Expected Output:
Reversed slice: [4, 3, 2, 1]
Q8. Write a Rust program to check if a slice contains a specific element.
Input:
Slice: [1, 2, 3, 4], Element: 3
Expected Output:
Slice contains 3.
Q9. Write a Rust program to copy a slice to a new vector.
Input:
Slice: [1, 2, 3]
Expected Output:
Copied vector: [1, 2, 3]
Q10. Write a Rust program to print elements of a slice using iteration.
Input:
Slice: [10, 20, 30]
Expected Output:
10 20 30
Q11. Write a Rust program to find the maximum value in a slice.
Input:
Slice: [5, 3, 9, 1]
Expected Output:
Maximum value: 9
Q12. Write a Rust program to find the minimum value in a slice.
Input:
Slice: [5, 3, 9, 1]
Expected Output:
Minimum value: 1
Q13. Write a Rust program to sum all the elements of a slice.
Input:
Slice: [1, 2, 3, 4]
Expected Output:
Sum: 10
Q14. Write a Rust program to check if a slice is sorted in ascending order.
Input:
Slice: [1, 2, 3, 4]
Expected Output:
Slice is sorted in ascending order.
Q15. Write a Rust program to sort a slice in ascending order.
Input:
Slice: [4, 3, 2, 1]
Expected Output:
Sorted slice: [1, 2, 3, 4]
Q16. Write a Rust program to find the index of an element in a slice.
Input:
Slice: [10, 20, 30, 40], Element: 30
Expected Output:
Index of 30: 2
Q17. Write a Rust program to create a slice from a vector and print its elements.
Input:
Vector: [1, 2, 3, 4, 5]
Expected Output:
Slice: [2, 3, 4]
Q18. Write a Rust program to create a slice from a range of indices in an array.
Input:
Array: [10, 20, 30, 40, 50], Range: 1..4
Expected Output:
Slice: [20, 30, 40]
Q19. Write a Rust program to find the sum of even numbers in a slice.
Input:
Slice: [1, 2, 3, 4, 5, 6]
Expected Output:
Sum of even numbers: 12
Q20. Write a Rust program to slice a string and print the result.
Input:
String: "hello", Start index: 1, End index: 4
Expected Output:
Slice: "ell"
Q21. Write a Rust program to create a mutable slice from a vector.
Input:
Vector: [10, 20, 30]
Expected Output:
Mutable slice: [10, 20, 30]
Q22. Write a Rust program to concatenate two slices.
Input:
Slice 1: [1, 2], Slice 2: [3, 4]
Expected Output:
Concatenated slice: [1, 2, 3, 4]
Q23. Write a Rust program to find the common elements between two slices.
Input:
Slice 1: [1, 2, 3], Slice 2: [3, 4, 5]
Expected Output:
Common elements: [3]
Q24. Write a Rust program to check if a slice is a sub-slice of another slice.
Input:
Slice 1: [1, 2, 3], Slice 2: [2, 3]
Expected Output:
Slice 2 is a sub-slice of Slice 1.
Q25. Write a Rust program to find the product of all elements in a slice.
Input:
Slice: [1, 2, 3, 4]
Expected Output:
Product: 24
Q26. Write a Rust program to remove duplicates from a slice.
Input:
Slice: [1, 2, 2, 3, 4, 4]
Expected Output:
Slice after removing duplicates: [1, 2, 3, 4]
Q27. Write a Rust program to create a slice from the middle of an array.
Input:
Array: [1, 2, 3, 4, 5], Start index: 1, End index: 4
Expected Output:
Slice: [2, 3, 4]
Q28. Write a Rust program to create a slice that omits the first and last elements of an array.
Input:
Array: [1, 2, 3, 4, 5]
Expected Output:
Slice: [2, 3, 4]
Q29. Write a Rust program to print a slice in a formatted way.
Input:
Slice: [10, 20, 30]
Expected Output:
Formatted slice: [10, 20, 30]
Q30. Write a Rust program to split a slice into two parts.
Input:
Slice: [1, 2, 3, 4, 5], Split index: 2
Expected Output:
First part: [1, 2], Second part: [3, 4, 5]
Q31. Write a Rust program to double each element in a slice.
Input:
Slice: [1, 2, 3]
Expected Output:
Doubled slice: [2, 4, 6]
Q32. Write a Rust program to merge two slices into one.
Input:
Slice 1: [1, 2], Slice 2: [3, 4]
Expected Output:
Merged slice: [1, 2, 3, 4]
Q33. Write a Rust program to compare two slices.
Input:
Slice 1: [1, 2, 3], Slice 2: [1, 2, 3]
Expected Output:
Slices are equal.
Q34. Write a Rust program to check if a slice contains all elements of another slice.
Input:
Slice 1: [1, 2, 3], Slice 2: [2, 3]
Expected Output:
Slice 1 contains all elements of Slice 2.
Q35. Write a Rust program to slice a string and convert it to uppercase.
Input:
String: "hello", Start index: 0, End index: 3
Expected Output:
Uppercase slice: "HEL"
Q36. Write a Rust program to convert a slice to a string.
Input:
Slice: ['h', 'e', 'l', 'l', 'o']
Expected Output:
String: "hello"
Q37. Write a Rust program to find the number of occurrences of a specific element in a slice.
Input:
Slice: [1, 2, 3, 1, 4], Element: 1
Expected Output:
Occurrences of 1: 2
Q38. Write a Rust program to find the median of a slice.
Input:
Slice: [1, 2, 3, 4, 5]
Expected Output:
Median: 3
Q39. Write a Rust program to slice a slice from a slice.
Input:
Slice: [10, 20, 30, 40, 50], Start index: 1, End index: 3
Expected Output:
Slice: [20, 30]
Q40. Write a Rust program to create a slice from a portion of a string.
Input:
String: "rust programming", Start index: 0, End index: 4
Expected Output:
Slice: "rust"
Q41. Write a Rust program to check if a slice is a prefix of another slice.
Input:
Slice 1: [1, 2, 3], Slice 2: [1, 2]
Expected Output:
Slice 2 is a prefix of Slice 1.
Q42. Write a Rust program to check if a slice is a suffix of another slice.
Input:
Slice 1: [1, 2, 3], Slice 2: [2, 3]
Expected Output:
Slice 2 is a suffix of Slice 1.
Q43. Write a Rust program to iterate over a slice and print each element.
Input:
Slice: [10, 20, 30]
Expected Output:
10
20
30
Q44. Write a Rust program to check if a slice contains only positive numbers.
Input:
Slice: [1, 2, 3, 4]
Expected Output:
Slice contains only positive numbers.
Q45. Write a Rust program to split a slice into chunks.
Input:
Slice: [1, 2, 3, 4, 5], Chunk size: 2
Expected Output:
Chunks: [[1, 2], [3, 4], [5]]
Q46. Write a Rust program to find the common elements in two slices.
Input:
Slice 1: [1, 2, 3], Slice 2: [3, 4, 5]
Expected Output:
Common elements: [3]
Q47. Write a Rust program to concatenate a string and a slice.
Input:
String: "Hello", Slice: [' ', 'R', 'u', 's', 't']
Expected Output:
Concatenated result: "Hello Rust"
Q48. Write a Rust program to remove the first element from a slice.
Input:
Slice: [1, 2, 3, 4]
Expected Output:
Slice after removing first element: [2, 3, 4]
Q49. Write a Rust program to remove the last element from a slice.
Input:
Slice: [1, 2, 3, 4]
Expected Output:
Slice after removing last element: [1, 2, 3]
Q50. Write a Rust program to slice a portion of a string and append it to another string.
Input:
String 1: "Rust", String 2: "Programming", Start index: 1, End index: 4
Expected Output:
New string: "RustProgra"