Hone logo
Hone
Problems

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:

  1. slice_sum(slice: &[i32]) -> i32: Calculates the sum of all elements in the input slice.
  2. slice_reverse(slice: &[i32]) -> Vec<i32>: Creates a new Vec<i32> containing the elements of the input slice in reversed order. Do not modify the original slice.
  3. slice_subslice(slice: &[i32], start: usize, end: usize) -> &[i32]: Creates a new slice from the input slice, starting at index start (inclusive) and ending at index end (exclusive). Handle out-of-bounds indices gracefully by returning an empty slice (&[]) if start or end are invalid. Ensure start is not greater than end.
  4. slice_filter(slice: &[i32], predicate: fn(i32) -> bool) -> Vec<i32>: Creates a new Vec<i32> containing only the elements from the input slice that satisfy the given predicate function.

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 i32 elements.
  • start and end indices for slice_subslice will be non-negative usize values.
  • The predicate function for slice_filter will take an i32 and return a bool.
  • The functions should be reasonably efficient; avoid unnecessary allocations or copies where possible. slice_subslice should 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_sum and slice_filter.
  • The slice_reverse function must return a new Vec<i32> and should not modify the original slice. This is important for maintaining ownership and borrowing rules.
  • The slice_filter function should also return a new Vec<i32>.
Loading editor...
rust