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 num in slice.iter_mut() { if *num % 2 == 0 { *num *= 2; } else { *num -= 1; } }}
pub fn transform_even_odd(slice: &mut [i32]) { for el in slice { 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 { 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. 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 slice.iter_mut() { if *i%2 == 0 { *i = 2 * *i; } else { *i = *i - 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. let slice_iter = slice.iter_mut(); for num in slice_iter { if *num % 2 == 0 { *num = *num * 2; }else{ *num = *num - 1; } }}
pub fn transform_even_odd(slice: &mut [i32]) { 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(mutate_number)}fn mutate_number(n: &mut i32) { 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(|val| { *val = 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 i in slice.iter_mut() { *i = 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 element in slice.iter_mut() { *element = 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.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 *= 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; continue; } *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 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 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 number in slice.iter_mut(){ if *number % 2 == 0 { *number = *number * 2; }else { *number = *number - 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 it in slice.iter_mut() { if *it % 2 == 0 { *it *= 2 } else { *it -= 1 } }}
pub fn transform_even_odd(slice: &mut [i32]) { if slice.is_empty() { () } 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 item in slice.iter_mut() { if *item % 2 == 0 { *item = *item * 2; } else { *item = *item - 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(|i| 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 == 0 { // leave zero as zero } else if *num % 2 == 0 { *num *= 2; // double non-zero evens } else { *num -= 1; // subtract one from odds } }}
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; // Double the value if it's even } else { *i -= 1; // Increment the value if it's odd } }}
pub fn transform_even_odd(slice: &mut [i32]) { slice.iter_mut().for_each(|i| if *i % 2 == 0 { *i *= 2 } else { *i -= 1 } );}
pub fn transform_even_odd(slice: &mut [i32]) { slice.iter_mut().for_each(|i| 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;} }}
fn is_even(n: &mut i32) -> bool { *n % 2 == 0}pub fn transform_even_odd(slice: &mut [i32]) { slice.iter_mut().for_each(|n| { *n = match is_even(n) { true => *n * 2, false => *n - 1, }; });}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. println!("num_slice: {:?}", slice); for n in slice.iter_mut() { if *n % 2 == 0 { *n *= 2 } else { *n -= 1 } }; println!("transfomed num_slice: {:?}", slice);}
pub fn transform_even_odd(slice: &mut [i32]) { // Your code here: iterate over the mutable slice and modify its elements. println!("num_slice: {:?}", slice); for n in slice.iter_mut() { if *n % 2 == 0 { *n *= 2 } else { *n -= 1 } }; println!("transfomed num_slice: {:?}", slice);}
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]) { 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 val_mut in slice.iter_mut(){ if *val_mut % 2 == 0 { *val_mut *= 2; } else{ *val_mut -= 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 *= 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 num in slice { if *num % 2 == 0 { *num *= 2; } else { *num -= 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 i in slice.iter_mut() { if *i % 2 == 0 { *i *= 2; } else { *i -= 1; } }}
pub fn transform_even_odd(slice: &mut [i32]) { for i in slice { 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 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 elem in slice { 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 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. 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. 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 slice.iter_mut(){ match *i{ n if (*i)%2 ==0 => *i = n*2, n => *i = n-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 *= 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 { 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 value in slice { if *value % 2 == 0 { *value *= 2; } else { *value -= 1; } }}