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 (i, v) in slice.iter_mut().enumerate() { if *v % 2 == 0 { *v *= 2; }else{ *v -= 1; } }}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. for (i, v) in slice.iter_mut().enumerate() { if *v % 2 == 0 { *v *= 2; }else{ *v -= 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]) { for s in slice.iter_mut() { if *s % 2 == 0 { *s = *s*2; } else { *s = *s-1; } }}
pub fn transform_even_odd(slice: &mut [i32]) { 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 elm in slice.iter_mut() { if *elm % 2 == 0 { *elm = *elm * 2; } else { *elm -= 1; } }}
pub fn transform_even_odd(slice: &mut [i32]) { for x in slice { if *x & 1 == 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(|el| if *el % 2 == 0 { *el *= 2 } else { *el -= 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]) { for ele in slice { if *ele % 2 == 0 { *ele *= 2; } if *ele % 2 != 0 { *ele -= 1; } }}
pub fn transform_even_odd(slice: &mut [i32]) { for num in slice { 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 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 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 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. 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 elem in slice.iter_mut() { if *elem % 2 == 0 { *elem *= 2; } else { *elem -= 1 } }}
pub fn transform_even_odd(slice: &mut [i32]) { for num in slice.iter_mut() { if *num % 2 == 0 { *num *= 2 } else { *num -= 1 } }}
pub fn transform_even_odd(slice: &mut [i32]) { for val in slice.iter_mut(){ if *val % 2 == 0 { *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. for ele in slice.iter_mut() { if *ele % 2 == 0 { *ele *= 2; } else { *ele -= 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 e in slice.iter_mut() { if *e%2==0 { *e*=2 } else { *e-=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(|v|{ if *v %2 == 0 { *v *= 2; } else { *v -= 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(|v|{ if *v %2 == 0 { *v *= 2; } else { *v -= 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(|e|{ if *e%2 == 0 { *e *= 2; }else{ *e-= 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] = slice[i]+slice[i]; } 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] = slice[i]+slice[i]; } else{ slice[i]-=1; } }}
pub fn transform_even_odd(slice: &mut [i32]) { for i in slice.iter_mut() { if *i % 2 == 0 { *i *= 2; // 偶数乘以2 } else { *i -= 1; // 奇数减去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 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 slice.iter_mut(){ 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 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 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 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. 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 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 i in slice.iter_mut() { 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 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 i in slice.iter_mut(){ if *i % 2 == 0{ *i = *i * 2; }else{ *i = *i - 1; } }}
pub fn transform_even_odd(slice: &mut [i32]) { 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 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 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. slice.iter_mut().for_each(|x| { if *x % 2 == 0 { *x *= 2; // Double the even numbers } else { *x -= 1; // Reduce the odd numbers } });}
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 elements in 0..slice.len(){ if slice[elements] %2 ==0{ slice[elements] *=2; } else{ slice[elements] -=1; } }}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. let transform_fn = |num: &mut i32| if *num % 2 == 0 { *num = *num*2 } else { *num = *num-1 }; slice.iter_mut().for_each(transform_fn);}
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 = *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 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 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 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 num in slice.iter_mut() { if *num % 2 == 0 { *num *= 2; } else { *num -= 1; } }}