I am trying to create an immutable array of user input in rust but the problem is that I have to initialize the array first, and if I initialize the array I cannot insert user inputs in the array.
fn main() {
//error: If I don't initialize the array then it throws and error.
let arr: [i32;5] = [0;5];
for i in 0..5 {
// let i be the user input for now
// if I do initialize the array I cannot reassign the array elements because of immutability
arr[i] = i as i32;
}
}
Error Message:
Compiling playground v0.0.1 (/playground)
error[E0594]: cannot assign to `arr[_]`, as `arr` is not declared as mutable
--> src/main.rs:7:9
|
3 | let arr: [i32;5] = [0;5];
| --- help: consider changing this to be mutable: `mut arr`
...
7 | arr[i] = i as i32;
| ^^^^^^^^^^^^^^^^^ cannot assign
For more information about this error, try `rustc --explain E0594`.
error: could not compile `playground` due to previous error
mutwhere the error suggests? If you really wantarrto be immutable you can writelet arr = arr;later. Is that what you want?