The compiler complains a variable is not initialized, and it is right. However, the variable appears on the left side of the expression.
I guess I could easily fix this by initializing the array, but I am more interested in understanding why the compiler thinks this is an error condition.
I don't think this would be flagged as an error in other languages.
Here is my code:
fn main() {
const LEN: usize = 5;
let mut arr: [u32; LEN];
for i in 0..LEN {
arr[i] = fib(i as u32);
}
println!("{:?}", arr);
}
fn fib(n: u32) -> u32 {
match n {
0 => 0,
1 => 1,
_ => fib(n - 1) + fib(n - 2),
}
}
Here is the error:
error[E0381]: use of possibly uninitialized variable: `arr`
--> src/main.rs:6:9
|
6 | arr[i] = fib(i as u32);
| ^^^^^^^^^^^^^^^^^^^^^^ use of possibly uninitialized `arr`
error[E0381]: use of possibly uninitialized variable: `arr`
--> src/main.rs:9:22
|
9 | println!("{:?}", arr);
| ^^^ use of possibly uninitialized `arr`