Please someone explain the code below and how can it be unsafe and why is borrowchecker complaining here? I am very new to rust and I come from a little bit of c/c++ background.
fn test(a: &mut [i32], place: usize) -> i32 {
a[place] /= 2;
return a[place];
}
fn main() {
let mut values = vec![1, 2, 3, 4];
let b = test(&mut values, values.len() / 2); // compiler gives error on values.len()
}
Why does it work with first assigning a variable with values.len()/2
and passing that to the function?