Mastering Slice Operations in Rust
Rust's slices are a powerful tool for working with sequences of data without taking ownership. This challenge will test your understanding of how to create and manipulate slices, demonstrating their flexibility and efficiency in accessing portions of arrays and vectors. Successfully completing this challenge will solidify your grasp of Rust's memory management and borrowing concepts.
Problem Description
You are tasked with implementing a set of functions that utilize Rust slices to perform common operations on arrays and vectors. The functions should accept a slice as input and return a new slice representing a modified or filtered version of the original data. You must handle edge cases gracefully, such as empty slices or invalid indices. The functions should be efficient and avoid unnecessary copying of data.
Specifically, you need to implement the following functions:
slice_sum(slice: &[i32]) -> i32: Calculates the sum of all elements in the input slice.slice_reverse(slice: &[i32]) -> Vec<i32>: Creates a newVec<i32>containing the elements of the input slice in reversed order. Do not modify the original slice.slice_subslice(slice: &[i32], start: usize, end: usize) -> &[i32]: Creates a new slice from the input slice, starting at indexstart(inclusive) and ending at indexend(exclusive). Handle out-of-bounds indices gracefully by returning an empty slice (&[]) ifstartorendare invalid. Ensurestartis not greater thanend.slice_filter(slice: &[i32], predicate: fn(i32) -> bool) -> Vec<i32>: Creates a newVec<i32>containing only the elements from the input slice that satisfy the givenpredicatefunction.
Examples
Example 1: slice_sum
Input: &[1, 2, 3, 4, 5]
Output: 15
Explanation: The sum of the elements 1 + 2 + 3 + 4 + 5 is 15.
Example 2: slice_reverse
Input: &[1, 2, 3]
Output: [3, 2, 1]
Explanation: The elements are reversed to create a new vector.
Example 3: slice_subslice
Input: &[1, 2, 3, 4, 5], 1, 3
Output: &[2, 3]
Explanation: A slice containing elements at indices 1 and 2 is returned.
Example 4: slice_subslice (Edge Case)
Input: &[1, 2, 3], 1, 5
Output: &[]
Explanation: The end index (5) is out of bounds, so an empty slice is returned.
Example 5: slice_filter
Input: &[1, 2, 3, 4, 5], |x| x % 2 == 0
Output: [2, 4]
Explanation: Only the even numbers (2 and 4) are included in the new vector.
Constraints
- The input slices will contain
i32elements. startandendindices forslice_subslicewill be non-negativeusizevalues.- The
predicatefunction forslice_filterwill take ani32and return abool. - The functions should be reasonably efficient; avoid unnecessary allocations or copies where possible.
slice_subsliceshould return a slice, not a copy. - The length of the input slice will be between 0 and 1000 (inclusive).
Notes
- Remember that slices are references to a contiguous sequence of elements in memory.
- Pay close attention to bounds checking when creating sub-slices.
- Consider using iterators for efficient processing of slice elements, especially in
slice_sumandslice_filter. - The
slice_reversefunction must return a newVec<i32>and should not modify the original slice. This is important for maintaining ownership and borrowing rules. - The
slice_filterfunction should also return a newVec<i32>.