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.
Write a function modify_elements(slice: &mut [i32])
that modifies the elements of a mutable slice of integers in the following way:
let mut numbers = [1, 2, 3, 4, 5];
modify_elements(&mut numbers);
// Odd numbers reduced by 1
// Even numbers doubled
assert_eq!(numbers, [0, 4, 2, 8, 4]);
let mut numbers = [10, 15, 20];
modify_elements(&mut numbers);
// 10 -> 20, 15 -> 14, 20 -> 40
assert_eq!(numbers, [20, 14, 40]);
If you're stuck, here are some hints to help you:
.iter_mut()
method to traverse the slice by mutable reference.num % 2 == 0
.*
) to update the value pointed to by the mutable reference.for
loop with mutable references for concise modification.pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. for elem in slice.iter_mut() { if *elem % 2 == 0 { *elem *= 2; } else { *elem -= 1; } }}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. for n in slice.iter_mut() { if *n %2 == 0 { *n *=2; } else{ *n -= 1; } }}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. for n in slice.iter_mut() { if *n %2 == 0 { *n *=2; } else{ *n -= 1; } }}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. slice.iter_mut().for_each(|x| if *x % 2 == 0 { *x *= 2; } else{ *x -= 1; } );}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. for s in slice { if *s % 2 == 0 { *s *= 2; } else { *s -= 1; } }}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. for num in slice.iter_mut() { if *num % 2 == 0 { *num *=2; } else { *num -= 1; } }}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. for number in slice.iter_mut(){ if *number % 2 == 0 { *number *= 2 }else { *number -= 1 } }}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. for iter in slice.iter_mut(){ if *iter % 2 == 0{ *iter *= 2 }else { *iter -= 1 } }}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. for iter in slice.iter_mut() { if *iter % 2 == 0 { *iter = *iter * 2; } else { *iter = *iter - 1 } }}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. slice.iter_mut().for_each(|x| { if *x % 2 == 0 { *x *= 2; } else { *x -= 1; } });}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. for item in slice.iter_mut() { if *item % 2 == 0 { *item *= 2; } else { *item -= 1; } }}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. for num in slice.iter_mut() { if *num % 2 == 0 { *num = *num * 2; } else { *num = *num - 1; } }}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. for i in 0 .. slice.len() { if slice[i] % 2 == 0 { slice[i] *= 2; } else { slice[i] -= 1; } }}
pub fn transform_even_odd(slice: &mut [i32]) { for elem in slice.iter_mut() { if *elem % 2 == 0 { *elem *= 2; } else { *elem -= 1; } }}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. let mut index:usize = 0; while index != slice.len() { if slice[index] % 2 == 0 { slice[index] = slice[index] * 2; }else { slice[index] = slice[index] - 1; } index += 1; }}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. slice.iter_mut().for_each(|x| { if *x % 2 == 0 { *x *= 2; } else { *x -= 1; } });}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. for item in slice.iter_mut() { if *item % 2 == 0 { *item *= 2; } else { *item -= 1; } }}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. for el in slice { if *el % 2 == 0 { *el = *el * 2; } else { *el = *el - 1; } }}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. for x in slice.iter_mut() { if *x % 2 == 0 { *x = *x*2; } else { *x = *x -1; } }}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. for x in 0..slice.len() { //if even if slice[x]%2 == 0 { slice[x] = slice[x] * 2 } else { slice[x] = slice[x] - 1 } } }
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. for element in slice { if *element % 2 == 0 { *element = *element * 2 } else { *element = *element - 1 } }}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. for element in slice { if *element % 2 == 0 { *element = *element * 2 } else { *element = *element - 1 } }}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. for element in slice { if *element % 2 == 0 { *element = *element * 2 } else { *element = *element - 1 } }}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. for element in slice { if *element % 2 == 0 { *element = *element * 2 } else { *element = *element - 1 } }}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. //slice.iter().filter(|x|x%2 == 0 ).map() for s in slice { if *s % 2 == 0 { *s = 2*(*s); }else { *s = *s - 1; } }}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. slice.iter_mut().for_each(|x| { if *x % 2 ==0 { *x *= 2; } else { *x -= 1; } });}
pub fn transform_even_odd(slice: &mut [i32]) { slice.iter_mut().for_each(|x| { if *x % 2 == 0 { *x *= 2; } else { *x -= 1; } })}
pub fn transform_even_odd(slice: &mut [i32]) { slice.iter_mut().for_each(|number| { if *number % 2 == 0 { *number *= 2; } else { *number -= 1; } });}
pub fn transform_even_odd(slice: &mut [i32]) { // for i in slice.iter_mut() { // if *i % 2 == 1 || *i % 2 == -1 { // *i -= 1; // } else { // *i *= 2; // } // } for i in 0..slice.len() { if slice[i] % 2 == 0 { slice[i] *= 2; } else { slice[i] -= 1; } }}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. for i in 0..slice.len() { if slice[i] % 2 == 0 { slice[i] *= 2; }else { slice[i] -= 1; } }}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. for i in slice.iter_mut() { if *i % 2 == 0 { *i *= 2; } else { *i -= 1; } }}
pub fn transform_even_odd(slice: &mut [i32]) { for x in slice.iter_mut(){ if *x % 2==0{ *x *=2; }else{ *x -=1 } }}
pub fn transform_even_odd(slice: &mut [i32]) { for value in slice.iter_mut() { if *value % 2 == 0 { *value *= 2; } else { *value -= 1; } }}
pub fn transform_even_odd(slice: &mut [i32]) { for number in slice.iter_mut(){ if *number % 2 == 0{ *number = *number * 2; continue; } *number = *number -1; }}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. for x in slice.iter_mut() { if *x % 2 == 0 { *x *= 2; } else { *x -= 1; } }}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. slice.iter_mut().for_each(|x| { match *x % 2 { 0 => *x *= 2, _ => *x -= 1 }; });}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements for element in slice.iter_mut() { if *element % 2 == 0{ *element *= 2; } else { *element -= 1; } }}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements for element in slice.iter_mut() { if *element % 2 == 0{ *element *= 2; } else { *element -= 1; } }}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. for num in slice { if *num % 2 == 0 { *num *= 2; // Even: double it } else { *num -= 1; // Odd: subtract 1 } }}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements for val in slice.iter_mut() { if *val % 2 == 0 { *val = *val *2; } else { *val -=1; } } }
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. //slice.iter_mut().map(|x| if *x % 2 == 0 {*x = *x * 2} else {*x = *x - 1}).collect() for item in slice.iter_mut() { if *item % 2 == 0 { *item *= 2; } else { *item -= 1; } }}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. for element in slice.iter_mut() { if *element % 2 == 0 { *element = *element * 2; } else { *element = *element - 1; } }}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. for element in slice.iter_mut() { if *element % 2 == 0 { *element *= 2; } else { *element -= 1; } }}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. for n in slice.iter_mut() { if *n % 2 == 0 { *n *= 2; } else { *n -= 1; } }}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. for i in slice.iter_mut(){ if *i%2==0{ *i*=2; }else{ *i-=1; } }}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. for num in slice.iter_mut() { if *num % 2 == 0 { *num *= 2; } else { *num -= 1; } }}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. for number in slice.iter_mut() { if *number % 2 == 0 { *number *= 2; } else { *number -= 1; } }}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. for i in slice { if *i % 2 == 0 { *i = *i * 2 } else { *i = *i - 1 } }}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. for i in slice{ if *i%2==0{ *i= *i * 2; }else if *i%2!=0{ *i=*i-1 } }}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. for element in slice.iter_mut() { if *element % 2 == 0 { *element *= 2; } else { *element -= 1; } }}