I want to pass an array to a function and change the content inside it:
fn change_value(mut arr: &[i32]) {
arr[1] = 10;
}
fn main() {
let mut arr: [i32; 4] = [1, 2, 3, 4];
change_value(&arr);
println!("this is {}", arr[1]);
}
I'm getting this error:
warning: variable does not need to be mutable
--> src/main.rs:2:17
|
2 | fn change_value(mut arr: &[i32]) {
| ----^^^
| |
| help: remove this `mut`
|
= note: `#[warn(unused_mut)]` on by default
error[E0594]: cannot assign to `arr[_]` which is behind a `&` reference
--> src/main.rs:3:5
|
2 | fn change_value(mut arr: &[i32]) {
| ------ help: consider changing this to be a mutable reference: `&mut [i32]`
3 | arr[1] = 10;
| ^^^^^^^^^^^ `arr` is a `&` reference, so the data it refers to cannot be written
warning: variable does not need to be mutable
--> src/main.rs:7:9
|
7 | let mut arr: [i32; 4] = [1, 2, 3, 4];
| ----^^^
| |
| help: remove this `mut`
I've been searching around, but I can't find anything.