In this challenge, you'll explore how to work with mutable slices in Rust. A mutable slice, &mut [T], allows you to modify the elements of a collection directly and efficiently without creating a new collection.
Slices are a fundamental part of Rust, providing a view into contiguous sequences of elements such as arrays or vectors. When working with slices, you can iterate over and manipulate the elements dynamically.
Your Task
Write a function modify_elements(slice: &mut [i32]) that modifies the elements of a mutable slice of integers in the following way:
- Double each even number in the slice.
- Subtract 1 from each odd number in the slice.
- Modify the elements directly without creating a new collection.
Example
let mut numbers = [1, 2, 3, 4, 5];modify_elements(&mut numbers);// Odd numbers reduced by 1// Even numbers doubledassert_eq!(numbers, [0, 4, 2, 8, 4]);let mut numbers = [10, 15, 20];modify_elements(&mut numbers);// 10 -> 20, 15 -> 14, 20 -> 40assert_eq!(numbers, [20, 14, 40]);
Hints
If you're stuck, here are some hints to help you:
<details> <summary>Additional Hints</summary>- Use the
.iter_mut()method to traverse the slice by mutable reference. - Remember that you can check if a number is even using
num % 2 == 0. - Use dereferencing (
*) to update the value pointed to by the mutable reference. - You can use a
forloop with mutable references for concise modification. - The slice directly modifies the original collection it is derived from, so no need to return anything.
← PreviousNext →
pub fn transform_even_odd(slice: &mut [i32]) {// Your code here: iterate over the mutable slice and modify its elements.}